In the following JSON example, how can I access item of list when I do not know the id of those elements in javascript.
{
"list":{
"93817":{
"item_id":"93817"
"url":"http://url.com",
"title":"Page Title"
},
"935812":{
"item_id":"935812",
"url":"http://google.com",
"title":"Google"
}
}
}
var jsonObject = JSON.parse(req.responseText);
var item = jsonObject.list[0] // This does not work
PS: I can not change the format of the JSON since it is created by a third part.
You can iterate over the response using the “for (i in obj)” Javascript construct. Beware that iterating in this manner may uncover properties from higher objects in the prototype chain (who knows what wringer your JSON object went through…), so you can be very explicit if you want, and check for that.