0

(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!

Connor S
  • 25
  • 7
  • 2
    How do you access the first one? – yuriy636 Nov 23 '16 at 00:16
  • @yuriy636 I'm not sure, whenever I try to access it like I would for any other part of the api (like xuid or state) it gives me this error in the console: ` Uncaught TypeError: Cannot read property 'id' of undefined `I can post my code if you'd like EDIT: radmation's way worked :) – Connor S Nov 23 '16 at 00:21
  • var obj = JSON.parse(JSON_DATA); console.log(obj.devices[0].titles[1].id) – kjonsson Nov 23 '16 at 00:23

1 Answers1

-1

This should get you on the right track (untested)

var obj = jQuery.parseJSON(YOUR_JSON);
obj->devices[0]->titles[1]->id // second id
Radmation
  • 1,486
  • 1
  • 13
  • 30