Connor S Connor S - 13 days ago 6
Javascript Question

How to get response from an API that returns same values with different answers?

(Sorry if the title doesn't make much sense, I couldn't figure out how to word it correctly)

So I'm trying to get the response from an API, but the API returns multiple responses with the same name.
Ex of a response:

{
"xuid": 2535436668322645,
"state": "Online",
"devices": [
{
"type": "XboxOne",
"titles": [
{
"id": 714681658,
"name": "Home",
"placement": "Background",
"state": "Active",
"lastModified": "2016-11-22T23:45:08.8296994Z"
},
{
"id": 74304278,
"activity": {
"richPresence": "Lvl 30 in Hudson Yards"
},
"name": "Tom Clancy's The Division",
"placement": "Full",
"state": "Active",
"lastModified": "2016-11-22T23:45:08.8296994Z"
}
]
}
]
}


So I was wondering, how would I be able to get the ID from the "second" response using JavaScript? (The one that says "Tom Clancy's The Division" in the name part) Thanks!

Answer

This should get you on the right track (untested)

var obj = jQuery.parseJSON(YOUR_JSON);
obj->devices[0]->titles[1]->id // second id
Comments