21

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?

3
  • console.log(dataJS.comments[0]); ? Commented Mar 2, 2011 at 21:21
  • Have you checked that the Content-Type of the response headers really are application/json? Commented Mar 2, 2011 at 21:22
  • @Anapprentice Could you please respond to the thread with the correct answer? Commented Apr 14, 2019 at 15:11

7 Answers 7

19

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

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

I suspect that would work

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

Comments

6

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.

2 Comments

No, it isn't built out of spec so no it's not 'incorrect', but what syntax are you wanting to use to access it?
ok that's all I needed to know. Thank you! I will accept once stackoverflow lets me...
3

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]

Comments

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

Comments

2

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;

Comments

1

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, {}).

Comments

-3

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?

12 Comments

Yes, jQuery evaluates the JSON.
implied by $.ajax and the jquery tag
I guessed $.ajax can be any framework, like $ is used generally by most of them $.ajax is a pretty basic combination, so it was not clear.
@Brian: Most of the time i agree, but parsing without it is just a waste of resources. - And the server is a trusted source, your JS code comes from there anyway.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.