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?

3
  • 1
    As a side note, properties in general an be accessed with array notation. Even with an object var myvar = { mykey: myvalue } you can do myvar[mykey]. You can event execute functions var myvar = { mykey: function(){...} } with myvar[mykey](). Commented Dec 21, 2011 at 9:34
  • @Didier G.: You mean myvar["mykey"]. Commented Dec 21, 2011 at 11:53
  • @EdgarBonet. Indeed! Sorry for the typoh. Commented Dec 21, 2011 at 12:02

7 Answers 7

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

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'.
1
json[0]["0"]

Not really much more to add to that.

Comments

1
var foo = [{"0":"US"}];
console.log(foo[0]["0"]);

Comments

1

In this case you'll retrieve with

 json[0]["0"]

Comments

1

you may acess the value with:

var json = [{"0":"US"}]
json[0]["0"]

Comments

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'

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.