我可以将 JS 变量用作 JSON 字符串化请求中的节点吗?



基本上,我正在尝试从BungieAPI中获取数据,到目前为止,我一直很成功。我正在尝试读取此 JSON 文件。http://prntscr.com/k3tin3

但是我不确定如何从 json 中抓取节点然后使用它来请求字符数据?

到目前为止,这是我最好的猜测,它显然不起作用,我错过了什么? :/

$.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);
}
});

目前抓取字符 Ids 工作正常,这是我无法工作的第二行。我是否需要再次拨打电话,而不是在同一次通话中拨打电话?

编辑:我正在尝试使用返回的结果变量char_slot_0:2305843009303234831。在新的 json 字符串化请求中作为节点放置。

为什么要串化传入的 json 对象?您应该能够仅使用常规属性访问器(点运算符(访问这些字段。这一行var char_slot_0_class = JSON.stringify(json.Response.characters.data.+ char_slot_0 +.classType);看起来像无效的javascript。

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);
}

在阅读了使用异端猴子评论中的变量动态访问对象属性的可能副本后,我找到了解决方案!谢谢!

最新更新