如何修剪JSON数组以获得特定值

2024-09-27 02:17:41 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我使用Echo Arena API,它以JSON格式提供了以下内容。我正在尝试获取当时比赛中所有用户的姓名,如图所示,这里有球员姓名:rnedds和更远的DarkCobra866。我怎样才能只知道名字而不知道其他信息呢?使用Python3

{
   "teams":[
      {
         "players":[
            {
               "name":"rnedds",
               "rhand":[
                  -3.3230002,
                  -1.2370001,
                  -18.701
               ],
               "playerid":0,
               "position":[
                  -2.7520001,
                  -0.96800005,
                  -18.622002
               ],
               "lhand":[
                  -2.414,
                  -1.5630001,
                  -18.487001
               ],
               "userid":1663152230440088,
               "stats":{ }
            },
            {
               "name":"DarkCobra866",
               "rhand":[
                  -5.3710003,
                  -1.978,
                  -7.5110002
               ],
               "playerid":4,
               "position":[
                  -5.5280004,
                  -1.3520001,
                  -7.4040003
               ],
               "lhand":[
                  -5.6520004,
                  -1.7540001,
                  -7.4020004
               ],
               "userid":2649496045086049,
               "stats":{ }
            }
         ]
      }
   ]
}

目前,对于API中的其他信息,我的代码如下所示

 matchdetails = {
    'echosessionid' : data['sessionid'],
    'echoclientname' : data['client_name'],
    'echogameclockdisplay' : data['game_clock_display'],
    'echogamestatus' : data['game_status']
    }
    currentMatchDetails = json.dumps(matchdetails)

Tags: nameapi信息gamedatastatsposition姓名
1条回答
网友
1楼 · 发布于 2024-09-27 02:17:41

将JSON字符串加载到字典中,如下所示:

import json

json_text = '''
{
   "teams":[
      {
         "players":[
            {
               "name":"rnedds",
               "rhand":[
                  -3.3230002,
                  -1.2370001,
                  -18.701
               ],
               "playerid":0,
               "position":[
                  -2.7520001,
                  -0.96800005,
                  -18.622002
               ],
               "lhand":[
                  -2.414,
                  -1.5630001,
                  -18.487001
               ],
               "userid":1663152230440088,
               "stats":{ }
            },
            {
               "name":"DarkCobra866",
               "rhand":[
                  -5.3710003,
                  -1.978,
                  -7.5110002
               ],
               "playerid":4,
               "position":[
                  -5.5280004,
                  -1.3520001,
                  -7.4040003
               ],
               "lhand":[
                  -5.6520004,
                  -1.7540001,
                  -7.4020004
               ],
               "userid":2649496045086049,
               "stats":{ }
            }
         ]
      }
   ]
}
'''

data = json.loads(json_text)

players = [player['name'] for team in data['teams'] for player in team['players']]

print(players)

上述代码将导致:

['rnedds', 'DarkCobra866']

相关问题 更多 >

    热门问题