【问题标题】:How to trim JSON array to get specific value如何修剪 JSON 数组以获取特定值
【发布时间】:2020-05-17 13:30:27
【问题描述】:

所以我正在使用 Echo Arena API,它以 JSON 格式为我提供了以下一些内容。我当时正试图在比赛中获得所有用户的 NAMES,如此处所示,有球员姓名:rnedds 和更远的 DarkCobra866。我怎样才能只得到名字而不得到其他信息?使用 Python 3。

{
   "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)

【问题讨论】:

  • 请正确格式化代码和json
  • json 应该通过类似jsonlint.com
  • 考虑如果json中有你不关心的信息:我们为什么要关心它?减少它会使问题更容易解决。
  • 好吧,抱歉,我是编程和 stackoverflow 的新手,希望我的编辑更好。

标签: python json python-3.x sorting


【解决方案1】:

将您的 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']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-08
    • 1970-01-01
    • 2022-01-11
    • 2021-03-25
    • 1970-01-01
    相关资源
    最近更新 更多