最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JSON.parse parsing JSON with nested objects - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share Improve this question edited Apr 14, 2013 at 21:51 user1106925 asked Apr 14, 2013 at 21:35 Dannyboy1430Dannyboy1430 1351 gold badge1 silver badge4 bronze badges 3
  • You'll have to quote how you're parsing that. If the JSON really is as shown, json[0].atco is the correct way to access the atco property of the first entry in the array. So that leaves us speculating about what's going wrong, which isn't useful to anyone. :-) – T.J. Crowder Commented Apr 14, 2013 at 21:42
  • please write compelete json script. – Sina R. Commented Apr 14, 2013 at 21:42
  • if json refers to that object as you've written it, it will work: jsfiddle.net/XmchJ Something else going on, I suspect "am not i am" has it figured out – Ben McCormick Commented Apr 14, 2013 at 21:43
Add a comment  | 

1 Answer 1

Reset to default 18

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.

发布评论

评论列表(0)

  1. 暂无评论