【问题标题】:How to get value from json with random ID如何从具有随机 ID 的 json 中获取值
【发布时间】:2017-07-09 13:11:32
【问题描述】:

我在从 JSON 中检索值时遇到问题

我不知道如何检索在 id 之后给出的值 [NAME](它是随机的)

    "allplayers": {
    "76561197979570214": {
        "clan": "FBÏ",
        "name": "phr",
        "observer_slot": 6,
        "team": "T",
    },
    "76561198156373160": {
        "clan": "ZOWIE",
        "name": "TOAO",
        "observer_slot": 7,
        "team": "T",
    },
    "76561198071702537": {
        "clan": "Team Biceps",
        "name": "snatchie",
        "observer_slot": 8,
        "team": "T",
    },
},

具有常数值

"map": {
    "mode": "competitive",
    "name": "de_overpass",
    "phase": "live",
    "round": 7,
    "team_ct": {
        "score": 7,
        "name": "Team Kinguin",
        "flag": "PL",
        "timeouts_remaining": 1,
        "matches_won_this_series": 0
    },
    "team_t": {
        "score": 0,
        "name": "Samsung4Gamers",
        "flag": "PL",
        "timeouts_remaining": 1,
        "matches_won_this_series": 0
    },
    "num_matches_to_win_series": 0,
    "current_spectators": 3,
    "souvenirs_last_round": 0,
    "souvenirs_total": 0
},

我正在使用

data = JSON.parse(msg);
$("#ct_score").text(data.map.team_ct.score);

谁能帮帮我?谢谢!

【问题讨论】:

  • 这把钥匙是否总是带有team字样?

标签: javascript json node.js decode


【解决方案1】:

您可以遍历结果对象的属性并检查当前属性是否是它自己的,如下所示:

   allplayers = {
    "76561197979570214": {
        "clan": "FBÏ",
        "name": "phr",
        "observer_slot": 6,
        "team": "T",
    },
    "76561198156373160": {
        "clan": "ZOWIE",
        "name": "TOAO",
        "observer_slot": 7,
        "team": "T",
    },
    "76561198071702537": {
        "clan": "Team Biceps",
        "name": "snatchie",
        "observer_slot": 8,
        "team": "T",
    },
};

for(var key in allplayers){
    if(allplayers.hasOwnProperty(key)){
        if(allplayers[key].hasOwnProperty('name')){
          console.log(allplayers[key].name);
        }		
    }
}

【讨论】:

    猜你喜欢
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    相关资源
    最近更新 更多