0

Basically i am trying to grab data from the Bungie API i've been successful up until now. I am trying to read this JSON file. http://prntscr.com/k3tin3

But i'm unsure of how to grab a node from the json then use it to request character data?

This has been my best guess so far and it clearly doesn't work, What am i missing? :/

$.ajax({
  url: "https://www.bungie.net/Platform/Destiny2/4/Profile/" + searchField + "/?components=100,200",
  headers: {
  "X-API-Key": apiKey
}
}).done(function(json){
if(JSON.stringify(json.Response.profile.data.characterIds[0])){
    var char_slot_0 = JSON.stringify(json.Response.profile.data.characterIds[0]).replace(/[\"\"]/g, "");
    var char_slot_0_class = JSON.stringify(json.Response.characters.data.+ char_slot_0 +.classType);
    }
});

Currently grabbing characterIds is working fine, It's the second line i can't get to work. Do i need to make another call instead of doing them in the same call?

Edit: I am trying to use the result variable char_slot_0 which returns: 2305843009303234831. To put as a node in the new json stringify request.

T3rr11
  • 57
  • 9

2 Answers2

0

Why are you stringifying the incoming json object? You should be able to access these fields with just the regular property accessor (dot operator). This line var char_slot_0_class = JSON.stringify(json.Response.characters.data.+ char_slot_0 +.classType); looks like invalid javascript.

jman93
  • 367
  • 3
  • 9
  • It very much is invalid, That why i couldn't figure out how to get it to work, Also i stringify because if i don't i get an [Object object] result. Stringifying is works and gives me a readable string. – T3rr11 Jul 08 '18 at 02:01
0
if(JSON.stringify(json.Response.profile.data.characterIds[0])){
  var char_slot_0 = JSON.stringify(json.Response.profile.data.characterIds[0]).replace(/[\"\"]/g, "");
  var char_slot_0_class = JSON.stringify(json.Response.characters.data[char_slot_0].classType);
}

After reading a possible duplicate of Dynamically access object property using variable from Heretic Monkey's Comment, I found the solution! Thank you!

T3rr11
  • 57
  • 9