1

Possible Duplicate:
I have a nested data structure / JSON, how can I access a specific value?

I want to show data of a JSON array, I tried this but it doesn't work :

var Content = [{
    "01":[{"text":"blablablablabla","foo":"abeille :o"}],
    "02":[{"text":"blobloblobloblo","fuuu":"bzzzz :)"}],
    "03":[{"text":"blibliblibli","fiii":"bzzoooo ;)"}]
}];
alert(Content.01.foo);

How to do this?

Community
  • 1
  • 1
Random78952
  • 1,520
  • 4
  • 26
  • 36

3 Answers3

9

You need quotes and array indices.

   //   v---index 0 of the Array
Content[0]["01"][0].foo
   //            ^---index 0 of the Array
   //     ^----^---square brackets and quotes
I Hate Lazy
  • 47,415
  • 13
  • 86
  • 77
  • 1
    To be clear, what's happening here is that every [] is an array (though in this case each one only has one element). – jonvuri Oct 31 '12 at 14:29
4

Content is an array containing an object, not an object.

Identifiers cannot start with a number, so you cannot use them to access a property that starts with a number. You have to use square bracket notation (which is based on strings) instead of the identifier based dot notation.

Each numeric property in your object contains an array containing an object, not an object.

alert(Content[0]['01'][0].foo)
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

You used useless []

object - {} array - []

For more details about sintax JSON please take a look here

Modify your code in this way

var Content = {
    "01":{"text":"blablablablabla","foo":"abeille :o"},
    "02":{"text":"blobloblobloblo","fuuu":"bzzzz :)"},
    "03":{"text":"blibliblibli","fiii":"bzzoooo ;)"}
};
alert(Content['01'].foo);
Anton Baksheiev
  • 2,211
  • 2
  • 14
  • 15