2015-11-29 58 views
0

我想從其他API調用特定的統計信息。如何從其他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

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

這似乎在嘗試輸入幾個diferent方式後,仍然可以給予同樣的不確定error.Even –

相關問題