如何区分具有相同键的两个json对象?

时间:2017-12-01 21:35:17

标签: javascript json

我需要找出如何区分具有相同密钥的两个json对象 - 我是编码的新手,如果有一些我不理解的东西很抱歉:)

所以在这个从暴乱游戏API中抓起的json我遇到了像#34; leagueName"或"排名"等多次,我如何区分这些?

    [
    {
        "queueType": "RANKED_SOLO_5x5",
        "hotStreak": false,
        "wins": 29,
        "veteran": false,
        "losses": 25,
        "playerOrTeamId": "9673463",
        "leagueName": "Ezreal's Weaponmasters",
        "playerOrTeamName": "Battlerwinner",
        "inactive": false,
        "rank": "II",
        "freshBlood": false,
        "leagueId": "a74b4b30-2dcb-11e7-bcd1-02953f14ecb7",
        "tier": "SILVER",
        "leaguePoints": 0
    },
    {
        "queueType": "RANKED_FLEX_SR",
        "hotStreak": false,
        "wins": 109,
        "veteran": false,
        "losses": 102,
        "playerOrTeamId": "9673463",
        "leagueName": "Hecarim's Elementalists",
        "playerOrTeamName": "Battlerwinner",
        "inactive": false,
        "rank": "V",
        "freshBlood": false,
        "leagueId": "73693cb0-700a-11e7-b5e7-02953f14ecb7",
        "tier": "GOLD",
        "leaguePoints": 64
    },
    {
        "queueType": "RANKED_FLEX_TT",
        "hotStreak": false,
        "wins": 13,
        "veteran": false,
        "losses": 17,
        "playerOrTeamId": "9673463",
        "leagueName": "Akali's Ravagers",
        "playerOrTeamName": "Battlerwinner",
        "inactive": false,
        "rank": "II",
        "freshBlood": false,
        "leagueId": "f50c71a0-af99-11e7-91bd-02edcaa02cd3",
        "tier": "BRONZE",
        "leaguePoints": 44
    }
]

我希望能够区分键的值并将它们放在单独的变量中。我搜索了一下,没有找到任何有用的东西,所以我想我会在这里问。

我尝试了一些我觉得可行的电话,但我刚刚返回未定义。

2 个答案:

答案 0 :(得分:1)

有助于查看您尝试过的示例,以及了解您要对数据执行的操作。您应该能够使用jquery“each”循环遍历值以检索值。

  $.each(json, function (index, element) {
    <take action on each element here>
  });

答案 1 :(得分:0)

您可以抓取其他对象中的键,如下所示:

var data = [
    {
        "queueType": "RANKED_SOLO_5x5",
         ...
        "leagueName": "Ezreal's Weaponmasters",
         ...
    },
    {
        "queueType": "RANKED_SOLO_5x5",
         ...
        "leagueName": "Ezreal's SoulMasters",
         ...
    }
];

console.log( data[0].leagueName ); // Ezreal's Weaponmasters
console.log( data[1].leagueName ); // Ezreal's SoulMasters