我正在尝试从其他API调用特定的统计信息。
{"5908":[{"name":"Riven's Cutthroats","tier":"CHALLENGER","queue":"RANKED_SOLO_5x5","entries":[{"playerOrTeamId":"5908","playerOrTeamName":"Dyrus","division":"I","leaguePoints":727,"wins":373,"losses":313,"isHotStreak":false,"isVeteran":true,"isFreshBlood":false,"isInactive":false}]},{"name":"Lee Sin's Soldiers","tier":"PLATINUM","queue":"RANKED_TEAM_5x5","entries":[{"playerOrTeamId":"TEAM-46c2da55-7d0d-11e5-91f5-c81f66ddabda","playerOrTeamName":"RetiredCrabbyPpl","division":"III","leaguePoints":0,"wins":4,"losses":2,"isHotStreak":false,"isVeteran":false,"isFreshBlood":false,"isInactive":false}]}]}
我希望获得"tier":
值(在这种情况下为Challenger),但仅当"queue":
类型为"RANKED_SOLO_5X5"
时才会显示。
function getStuff() {
var SUMMONER_ID = "";
SUMMONER_ID = $('#theKey').val();
if(SUMMONER_ID !== 0) {
$.ajax({
url: 'https://na.api.pvp.net/api/lol/na/v2.5/league/by-summoner/' + SUMMONER_ID + '/entry?api_key={KEY}' ,
type: 'GET',
dataType: 'json',
async: false,
data: {
},
success: function (json) {
var user = SUMMONER_ID;
var summonerRank = json[user].tier;
document.getElementById("sRank").innerHTML = summonerRank;
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("error getting Summoner rank!");
}
});
} else{}
}
现在我有这个代码,但我唯一得到的是未定义的。 (我使用的密钥已被隐藏)。
答案 0 :(得分:0)
json[user]
处的值是一个数组。您的代码当前正在尝试访问数组对象的名为tier
的属性,而不是数组中的一个对象。
您需要循环遍历数组,然后测试每个对象的值
for(var i=0; i<json[user].length; i++){
if(json[user][i].queue == 'RANKED_SOLO_5X5'){
console.log(json[user][i].tier);
}
}