2

Possible Duplicate:
Parsing JSON with JavaScript

I understand to get the value of a json data, for instance var json = [{"country":"US"}], I can do it like json[0].country.

I've this json data [{"0":"US"}], so how do I retrieve the data then?

Community
  • 1
  • 1
ptamzz
  • 8,835
  • 31
  • 88
  • 144

7 Answers7

3

You could use json[0]['0'] as the "0" is just a name as far as JavaScript is concerned

isNaN1247
  • 17,513
  • 12
  • 69
  • 116
  • 3
    Note that `json[0][0]` (notice lack of quotes) will also work. The bracket notation converts everything in the brackets to a string, so `0` will be turned to `'0'`. – Zirak Dec 21 '11 at 13:38
1
json[0]["0"]

Not really much more to add to that.

Jamiec
  • 128,537
  • 12
  • 134
  • 188
1
var foo = [{"0":"US"}];
console.log(foo[0]["0"]);
Interrobang
  • 16,489
  • 3
  • 53
  • 62
1

In this case you'll retrieve with

 json[0]["0"]
Fabrizio Calderan
  • 115,126
  • 25
  • 163
  • 167
1

you may acess the value with:

var json = [{"0":"US"}]
json[0]["0"]
fyr
  • 19,441
  • 6
  • 35
  • 53
1

Here the key of the only object into the array is string so you can access it with:

var bar = [{"0":"US"}];
console.log(bar[0]['0']); // 'US'
Minko Gechev
  • 24,430
  • 7
  • 59
  • 67
1

If I'm understanding your question correctly, it would just be

json[0]["0"]

to retrieve your data. The zero is in quotes the second time round, because it's stored as a string in your example.

Herman Schaaf
  • 43,185
  • 19
  • 96
  • 137