AnApprentice
AnApprentice

Reputation: 111080

How to access an array in a JSON object?

I have the following JSON object:

[
    {
        "comments": [
            {
                "created_at": "2011-02-09T14:42:42-08:00",
                "thumb": "xxxxxxx",
                "level": 1,
                "id": 214,
                "user_id": 41,
                "parent_id": 213,
                "content": "<p>xxxxxx</p>",
                "full_name": "xx K"
            },
            {
                "created_at": "2011-02-09T14:41:23-08:00",
                "thumb": "xxxxxxxxxxxxx",
                "level": 0,
                "id": 213,
                "user_id": 19,
                "parent_id": null,
                "content": "<p>this is another test</p>",
                "full_name": "asd asd asd asd asd"
            }
        ],
        "eee1": "asdadsdas",
        "eee2": "bbbbb"
    }
]

This is coming from a $.ajax request, in success I have....

success: function (dataJS) {
    console.log(dataJS);
    console.log(dataJS[eee1]);
    console.log(dataJS.comments);
}

Problem is I can't get access to the items in the JSON object, even though dataJS does show correctly in the console. Ideas?

Upvotes: 21

Views: 68153

Answers (7)

Darko
Darko

Reputation: 38910

That's because your base object is an array as well.

console.log(dataJS[0].comments[0]);

I suspect that would work

Upvotes: 19

vbence
vbence

Reputation: 20343

JSON must be interpreted with eval function (after the obvious sanitization, see security considerations of eval). Are you sure your framework does that for you?

Upvotes: -3

Ryan Miller
Ryan Miller

Reputation: 391

Yes, as others have stated, the JSON is actually an Array (of a single Object). So you will need to reference an index.

Interestingly enough (to me), your result string does validate successfully as JSON. I assumed until now, that to be valid JSON, it had to be an Object (ie, {}).

Upvotes: 1

limc
limc

Reputation: 40176

Do something like this:-

var dataJS = [{"comments":[{"created_at":"2011-02-09T14:42:42-08:00","thumb":"xxxxxxx","level":1,"id":214,"user_id":41,"parent_id":213,"content":"<p>xxxxxx</p>","full_name":"xx K"},{"created_at":"2011-02-09T14:41:23-08:00","thumb":"xxxxxxxxxxxxx","level":0,"id":213,"user_id":19,"parent_id":null,"content":"<p>this is another test</p>","full_name":"asd asd asd asd asd"}],"eee1":"asdadsdas","eee2":"bbbbb"}];

var created_at = dataJS[0].comments[0].created_at;

Upvotes: 2

Detect
Detect

Reputation: 2069

console.log(dataJS);
console.log(dataJS[0].eee1);
console.log(dataJS[0].comments[0]);

Upvotes: 3

Brian Driscoll
Brian Driscoll

Reputation: 19635

The object being returned is itself an array, so to get to the first comment (as an example), this is how you would access it:

dataJS[0].comments[0]

Upvotes: 3

jondavidjohn
jondavidjohn

Reputation: 62412

the JSON you have coming back is actually an array itself, so...

dataJS[0].comments[0].created_at

will be 2011-02-09T14:42:42-08:00, etc...

Both dataJS and comments are arrays, and need indexes to access the appropriate elements.

Upvotes: 6

Related Questions