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
  • 9,235
  • 31
  • 91
  • 147

7 Answers7

3

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

isNaN1247
  • 17,793
  • 12
  • 71
  • 118
  • 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
  • 133,658
  • 13
  • 134
  • 193
1
var foo = [{"0":"US"}];
console.log(foo[0]["0"]);
Interrobang
  • 16,984
  • 3
  • 55
  • 63
1

In this case you'll retrieve with

 json[0]["0"]
Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177
1

you may acess the value with:

var json = [{"0":"US"}]
json[0]["0"]
fyr
  • 20,227
  • 7
  • 37
  • 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
  • 25,304
  • 9
  • 61
  • 68
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
  • 46,821
  • 21
  • 100
  • 139