如何从休息api调用特定的东西

时间:2022-10-16 13:31:57

I'm trying to call a specific stat from a rest API.

我正在尝试从rest API调用特定的stat。

    {"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}]}]}

I want to get the "tier": value (Challenger in this case), but only if the "queue": type is "RANKED_SOLO_5X5".

我想得到“层”:值(在这种情况下是挑战者),但仅当“队列”:类型为“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{}
}

right now i have this code but the only thing I get back is undefined. (The key I use has been hidden).

现在我有这个代码,但我唯一得到的是未定义的。 (我使用的密钥已被隐藏)。

1 个解决方案

#1


0  

The value at json[user] is an array. What your code is currently doing is trying to access a property named tier of the array object instead of one of the objects in the array.

json [user]的值是一个数组。您的代码当前正在尝试访问数组对象的名为tier的属性而不是数组中的某个对象。

You need to loop through the array and then test each object for your value

您需要遍历数组,然后测试每个对象的值

for(var i=0; i<json[user].length; i++){
   if(json[user][i].queue == 'RANKED_SOLO_5X5'){
      console.log(json[user][i].tier);
   }
}

#1


0  

The value at json[user] is an array. What your code is currently doing is trying to access a property named tier of the array object instead of one of the objects in the array.

json [user]的值是一个数组。您的代码当前正在尝试访问数组对象的名为tier的属性而不是数组中的某个对象。

You need to loop through the array and then test each object for your value

您需要遍历数组,然后测试每个对象的值

for(var i=0; i<json[user].length; i++){
   if(json[user][i].queue == 'RANKED_SOLO_5X5'){
      console.log(json[user][i].tier);
   }
}