JSON.parse parsing JSON with nested objects

I am trying to parse a JSON string with nested objects received in response to a send request. After running JSON.parse(responseText) result will be 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 that I could then infer the values ​​using the following values ​​as an example, but all I get is undefined.

 var atco = json[0].atco; 

I also tried json[0][0] , but it returns an individual character from JSON ( [ ). Does this mean that JSON is not correctly parsed or is this the expected behavior, and I'm just referring incorrectly?

+8
json javascript
source share
1 answer

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

As evidence, after you take it apart, analyze it again.

 var parsed = JSON.parse(resposneText); var parsed2 = JSON.parse(parsed); alert(parsed2.atco); 

Either this, or you parse it, and then try to select data from the source string. This obviously won't work.

+10
source share

All Articles