Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
57
rated 0 times [  64] [ 7]  / answers: 1 / hits: 28994  / 11 Years ago, sun, april 14, 2013, 12:00:00

I'm attempting to parse a JSON string with nested objects received in the response of a post request. After running JSON.parse(responseText), the result is in the following format:



[{
atco:43000156407,
location:{
longitude:-1.7876500000000000,
latitude:52.4147200000000000,
timestamp:2013-03-19 11:30:00
},
name:Solihull Station Interchange,
road:STATION APPROACH,
direction:NA,
locality:Solihull,
town:Solihull}, ...


I thought I would then be able pull values out using the following as an example, but all I get is undefined.



var atco = json[0].atco;


I've also tried json[0][0] but that returns an individual character from the JSON ([) . Does this indicate the JSON hasn't parsed correctly, or is this expected behaviour and I'm just referencing incorrectly?


More From » json

 Answers
19

This means that your JSON is being double encoded. Make sure you only encode it once on the server.



As proof, after you've parsed it, parse it again.



var parsed = JSON.parse(resposneText);

var parsed2 = JSON.parse(parsed);

alert(parsed2.atco);





Either that, or you're parsing it but then trying to select the data from the original string. This would obviously not work.


[#78915] Friday, April 12, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
collinisaaka

Total Points: 194
Total Questions: 105
Total Answers: 104

Location: Tonga
Member since Tue, Nov 30, 2021
3 Years ago
;