If you are asking a question, please follow this template:
My goal is:
I want to get element from JSON obj like “ip = jsonObj.ip” after “jsonObj = JSON.parse(text)”.
My actions are: my code is as followings.
let parse = function(response) {
let text = JSON.stringify(response);
print(“Geolocation responseText: “,text);
//text = text.replace(/(<([^>]+)>)/ig,””);
//text = ‘[’ + text + ‘]’;
let jsonObj = JSON.parse(text);
print("Geolocation jsonObj: ",jsonObj);
print(jsonObj.ip);
print(JSON.parse(jsonObj.ip));
ipGeoLoc.ip = jsonObj.ip;
ipGeoLoc.city = jsonObj.city;
}
The result I see is:
The error occurred at the line of “print(jsonObj.ip);” and another error also occurred at “print(JSON.parse(jsonObj.ip));”
The error log is as followings:
[Jun 2 17:38:31.436] Geolocation responseText: “{\n “ip”: “122.47.26.50”,\n “city”: “Sogongdong”,\n “region”: “Seoul-t’ukpyolsi”,\n “country”: “KR”,\n “loc”: “37.5645,126.9750”,\n “org”: “AS17858 LG POWERCOMM”\n}”
[Jun 2 17:38:31.491] jsonObj: {\x0a “ip”: “122.47.26.50”,\x0a “city”: “Sogongdong”,\x0a “region”: “Seoul-t’ukpyolsi”,\x0a “country”: “KR”,\x0a “loc”: “37.5645,126.9750”,\x0a “org”: “AS17858 LG POWERCOMM”\x0a}
[Jun 2 17:38:31.491] at ip-location.js:26
[Jun 2 17:38:31.492] at ip-location.js:51
[Jun 2 17:38:31.492] at api_http.js:102
[Jun 2 17:38:31.492] at api_net.js:58
[Jun 2 17:38:31.493] MJS callback error: type error
My expectation & question is:
The errored code of “print(jsonObj.ip);” or “print(JSON.parse(jsonObj.ip));” should output “122.47.26.50”.
I suspect that mJS dose not support access such as jsonObj.ip then please let me know how to get the elements in jsonObj.
cpq
June 3, 2019, 8:39am
2
It is very hard to figure out where the problem actually is - your example code is very confusing and unclear. Please tidy up, make it clear and minimal.
My code in ip-geoloc.js is as followings:
mocode:
let parse = function(response) {
let text = JSON.stringify(response);
print(“Geolocation responseText: “,text);
//text = text.replace(/(<([^>]+)>)/ig,””);
//text = ‘[’ + text + ‘]’;
let jsonObj = JSON.parse(text);
print("Geolocation jsonObj: ",jsonObj);
print(jsonObj.ip); <-- mJS error: no print output (I suspect that access to jsonObj.ip is unrecognizd.
print(JSON.parse(jsonObj.ip)); <-- also, no print output( Access to JSON.parse(jsonObj.ip) is unrecognized.
let ip = jsonObj.ip; <------------- mJS error
let city = jsonObj.city; <------------ mJS error
}
The error log of above function code:
mocode:
[Jun 2 17:38:31.436] Geolocation responseText: “{\n “ip”: “122.47.26.50”,\n “city”: “Sogongdong”,\n “region”: “Seoul-t’ukpyolsi”,\n “country”: “KR”,\n “loc”: “37.5645,126.9750”,\n “org”: “AS17858 LG POWERCOMM”\n}”
[Jun 2 17:38:31.491] Geolocation jsonObj: {\x0a “ip”: “122.47.26.50”,\x0a “city”: “Sogongdong”,\x0a “region”: “Seoul-t’ukpyolsi”,\x0a “country”: “KR”,\x0a “loc”: “37.5645,126.9750”,\x0a “org”: “AS17858 LG POWERCOMM”\x0a}
[Jun 2 17:38:31.491] at ip-location.js:26 <----- this is at the line of code “print(jsonObj.ip);”
[Jun 2 17:38:31.492] at ip-location.js:51
[Jun 2 17:38:31.492] at api_http.js:102
[Jun 2 17:38:31.492] at api_net.js:58
[Jun 2 17:38:31.493] MJS callback error: type error
please let me know how to get the element in JSON object on mJS.
and please show me the example.
You don’t need JSON.stringify(response)
. The respnse is already a json string.
Just JSON.parse(response)
1 Like