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.