从具有相同对象字符串名称的JSON对象获取valurs

时间:2015-05-08 02:20:11

标签: json

我从RIOT游戏API获取我的JSON对象。这是我回来的JSON的一部分:

{
"modifyDate": 1431043986000,
"champions": [
  {
     "id": 110,
     "stats": {
        "totalDeathsPerSession": 9,
        "totalSessionsPlayed": 1,
        "totalDamageTaken": 16507,
        "totalQuadraKills": 0,
        "totalTripleKills": 0,
        "totalMinionKills": 164,
        "maxChampionsKilled": 2,
        "totalDoubleKills": 0,
        "totalPhysicalDamageDealt": 101747,
        "totalChampionKills": 2,
        "totalAssists": 2,
        "mostChampionKillsPerSession": 2,
        "totalDamageDealt": 105995,
        "totalFirstBlood": 0,
        "totalSessionsLost": 1,
        "totalSessionsWon": 0,
        "totalMagicDamageDealt": 4248,
        "totalGoldEarned": 7932,
        "totalPentaKills": 0,
        "totalTurretsKilled": 0,
        "mostSpellsCast": 0,
        "maxNumDeaths": 9,
        "totalUnrealKills": 0
     }
  },
  {
     "id": 113,
     "stats": {
        "totalDeathsPerSession": 6,
        "totalSessionsPlayed": 1,
        "totalDamageTaken": 39226,
        "totalQuadraKills": 0,
        "totalTripleKills": 0,
        "totalMinionKills": 75,
        "maxChampionsKilled": 5,
        "totalDoubleKills": 1,
        "totalPhysicalDamageDealt": 28711,
        "totalChampionKills": 5,
        "totalAssists": 8,
        "mostChampionKillsPerSession": 5,
        "totalDamageDealt": 177132,
        "totalFirstBlood": 0,
        "totalSessionsLost": 1,
        "totalSessionsWon": 0,
        "totalMagicDamageDealt": 120299,
        "totalGoldEarned": 12544,
        "totalPentaKills": 0,
        "totalTurretsKilled": 1,
        "mostSpellsCast": 0,
        "maxNumDeaths": 6,
        "totalUnrealKills": 0
     }
  },
对于游戏中的所有冠军来说,等等(大约有150个)。如果我想计算总赢/输率或百分比,我必须找到“totalSessionsLost”和“totalSessionsWon”的总和。我将如何总结所有这些值?

由于

1 个答案:

答案 0 :(得分:1)

var total_win = 0,
    total_lost = 0,
    champions = yourjsonobj.champions;

for (var i = 0; i < champions.length; i++) {
    total_win += champions[i].totalSessionsWon;
    total_lost += champions[i].totalSessionsLost;
}