从深度嵌套的JSON中提取数据

时间:2020-07-17 18:33:51

标签: python arrays json dictionary

我是JSON数据的新手,我试图深入研究一个嵌套很深的字典,该字典位于3个数组和一个JSON文件的字典中。我正在尝试使用带有标签“ player”的字典。我正在尝试提取所有团队的球员,然后转移到DataFrame中,以便进行其他分析。

我该如何编写循环以获取此数据?

这是我要尝试执行的操作,但不起作用:

rostered = json.dumps(rostered_players)
player_value = [[
                values['playerPoolEntry']['player']
                ]for values in rostered]

以下是我正在使用的数据示例:

rostered = 
'''[
  [    
    {
    "appliedStatTotal": 260.05,
    "entries": [
        [
            {
            "injuryStatus": "NORMAL",
            "lineupSlotId": 2,
            "playerId": 2977644,
            "playerPoolEntry": {
                                "appliedStatTotal": 37.6,
                                "id": 2977644,
                                "keeperValue": 56,
                                "keeperValueFuture": 56,
                                "lineupLocked": false,
                                "onTeamId": 1,
                                "player":   {
                                            "firstName": "Todd",
                                            "fullName": "Todd Gurley II",
                                            "id": 2977644,
                                            "injuryStatus": "ACTIVE",
                                            "lastName": "Gurley II"
                                            },
            "rosterLocked": false,
            "tradeLocked": false
                                }
                }
            ]
        ]
    }
],
    [
        {
            "appliedStatTotal": 260.05,
            "entries": [
            [
                {
                "injuryStatus": "NORMAL",
                "lineupSlotId": 4,
                "playerId": 3892889,
                "playerPoolEntry": {
                                "appliedStatTotal": 6.6000000000000005,
                                "id": 3892889,
                                "keeperValue": 5,
                                "keeperValueFuture": 5,
                                "lineupLocked": false,
                                "onTeamId": 2,
                                "player":   {
                                            "defaultPositionId": 3,
                                            "firstName": "Dede",
                                            "fullName": "Dede Westbrook",
                                            "id": 3892889,
                                            "lastName": "Westbrook"
                                            },
            "rosterLocked": false,
            "tradeLocked": false
                                    }
                    }
                ]
            ]
        }
    ]
]'''

2 个答案:

答案 0 :(得分:-1)

执行此操作的一种方法是使用递归。这是一种更实用的方法。

from pprint import pprint

def iterate_deep(item):
  if isinstance(item, dict):
    for key, val in item.items():
      if key == 'player':
        pprint(val)
      else:
        iterate_deep(val)
  elif isinstance(item, list):
    for deeper_item in item:
      iterate_deep(deeper_item)

iterate_deep(rostered_players)

答案 1 :(得分:-1)

可能的解决方案可能是按原样解析给定的JSON。

rostered=json.loads(rostered)
    player_values=[]
    for level0 in rostered:
        for level1 in level0:
            if 'entries' in level1:
                level2 = level1.get('entries')
                for level3 in level2:
                    for level4 in level3:
                        if 'playerPoolEntry' in level4 and 'player' in level4.get('playerPoolEntry'):
                            player_values.append(level4.get('playerPoolEntry').get('player'))