我有一个函数可以访问 Twitch.tv API 以检索有关直播的各种信息。当我尝试检索在线流的“游戏” key 时,它只是返回“未定义”,尽管如果我访问开发人员控制台,我可以看到记录的 json 对象包含正确的游戏 key 。如何正确获取游戏 key ?
我的功能:
$("#usernamelookupcheck").on("click", function() {
$.getJSON("https://wind-bow.glitch.me/twitch-api/users/" + $("#usernamelookup").val(), function(json) {
if (json["error"] == "Not Found") {
$streamstatus2 = "User does not exist!";
$("#lookupresult").html("User does not exist!");
} else {
$.getJSON("https://wind-bow.glitch.me/twitch-api/streams/" + $("#usernamelookup").val(), function(json) {
$currentgame = json["game"];
if (json["stream"] === null) {
$streamstatus2 = "Offline";
} else {
$streamstatus2 = "Online" + "<br><br>";
console.log(json);
};
$("#lookupresult").html("<br><br> <strong>Status: " + $streamstatus2 + "</strong><br><br>" + $currentgame);
});
};
})
});
最佳答案
你需要使用
json['stream']['game']
或 json.stream.game
关于javascript - 如何在 json 对象中访问此键? (javascript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45025478/