2017-07-09 28 views
0

我有一个问题,从JSON如何从JSON值随机ID

检索值

我不知道如何检索值[NAME]是ID(这是随机的)

后给予
"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); 

任何人都可以帮助我吗?谢谢!

+0

将此密钥总是有** **队的字连接到它? –

回答

0

可以遍历您导致对象的属性和检查当前propertie是它自己的,就像这样:

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); 
 
     } \t \t 
 
    } 
 
}