How to access an array in a JSON object?
Solution 1
That's because your base object is an array as well.
console.log(dataJS[0].comments[0]);
I suspect that would work
Solution 2
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.
Solution 3
console.log(dataJS);
console.log(dataJS[0].eee1);
console.log(dataJS[0].comments[0]);
Solution 4
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]
Solution 5
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, {}).
AnApprentice
working on Matter, a new way to gather professional feedback.
Updated on July 09, 2022Comments
-
AnApprentice almost 2 years
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?
-
BoltClock about 13 yearsYes, jQuery evaluates the JSON.
-
jondavidjohn about 13 yearsimplied by
$.ajax
and the jquery tag -
jondavidjohn about 13 yearsNo, it isn't built out of spec so no it's not 'incorrect', but what syntax are you wanting to use to access it?
-
AnApprentice about 13 yearsok that's all I needed to know. Thank you! I will accept once stackoverflow lets me...
-
vbence about 13 yearsI 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.
-
vbence about 13 years@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.
-
user113716 about 13 years
$.ajax
does not necessarily imply that the data returned has been parsed. -
Brian Driscoll about 13 years@vbence: hmm... you should probably read up on
JSON.parse()
-
user113716 about 13 years@Brian: The json2 library uses
eval
. github.com/douglascrockford/JSON-js/blob/master/json2.js#L466 -
user113716 about 13 years@jondavidjohn: OP didn't specify what was meant by that. Could have been shown as a String.
-
Brian Driscoll about 13 years@patrick dw: yes, but only after it sanitizes the string. Thus,
JSON.parse()
>eval()
-
user113716 about 13 years@Brian: Yeah, I know. That's really the point. There are proper and improper uses of
eval
. -
vbence about 13 years@Brian: The JSON library is still eval, it only checks for obvious signs of functions thru regex. As a rule of thumb I agree that using eval on unchecked data is bad, but it does not change the fact that is parsed by the eval function.
-
vbence about 13 yearsAs the post was kind of vague, I added a clause for security considerations. Hope no more misunderstandings.