如何提取可变的json键的值

时间:2019-12-16 21:38:47

标签: python json python-3.x

我正在创建一个网站,向用户提供有关足球比赛的数据。对于每场比赛,我需要提取有关参加比赛的每个团队的阵容的数据。我从externall API请求阵容数据,该API以json格式向我提供此数据

data = {"api": {"results": 2, "lineUps": {"Paris Saint Germain": {"formation": "4-2-3-1", "startXI": [{"team_id": 85, "player_id": 253, "player": "Alphonse Ar\u00e9ola", "number": 16, "pos": "G"}, {"team_id": 85, "player_id": 256, "player": "Dani Alves", "number": 13, "pos": "M"}, {"team_id": 85, "player_id": 257, "player": "Marquinhos", "number": 5, "pos": "D"}, {"team_id": 85, "player_id": 262, "player": "Presnel Kimpembe", "number": 3, "pos": "D"}, {"team_id": 85, "player_id": 258, "player": "Juan Bernat", "number": 14, "pos": "D"}, {"team_id": 85, "player_id": 271, "player": "Leandro Paredes", "number": 8, "pos": "M"}, {"team_id": 85, "player_id": 273, "player": "Marco Verratti", "number": 6, "pos": "M"}, {"team_id": 85, "player_id": 267, "player": "Julian Draxler", "number": 23, "pos": "M"}, {"team_id": 85, "player_id": 276, "player": "Neymar", "number": 10, "pos": "F"}, {"team_id": 85, "player_id": 266, "player": "\u00c1ngel Di Mar\u00eda", "number": 11, "pos": "F"}, {"team_id": 85, "player_id": 274, "player": "Edinson Cavani", "number": 9, "pos": "F"}], "substitutes": [{"team_id": 85, "player_id": 254, "player": "Gianluigi Buffon", "number": 1, "pos": "G"}, {"team_id": 85, "player_id": 265, "player": "Arthur Zagre", "number": 33, "pos": "D"}, {"team_id": 85, "player_id": null, "player": "Loic Mbe Soh", "number": 36, "pos": "D"}, {"team_id": 85, "player_id": 260, "player": "Colin Dagba", "number": 31, "pos": "D"}, {"team_id": 85, "player_id": 263, "player": "Layvin Kurzawa", "number": 20, "pos": "D"}, {"team_id": 85, "player_id": 277, "player": "Moussa Diaby", "number": 27, "pos": "M"}, {"team_id": 85, "player_id": 269, "player": "Christopher Nkunku", "number": 24, "pos": "M"}]}, "Nice": {"formation": "4-3-3", "startXI": [{"team_id": 84, "player_id": 22157, "player": "Walter Ben\u00edtez", "number": 40, "pos": "G"}, {"team_id": 84, "player_id": 22161, "player": "Patrick Burner", "number": 15, "pos": "D"}, {"team_id": 84, "player_id": 22164, "player": "Christophe Herelle", "number": 29, "pos": "D"}, {"team_id": 84, "player_id": 22163, "player": "Dante", "number": 31, "pos": "D"}, {"team_id": 84, "player_id": 22166, "player": "Malang Sarr", "number": 23, "pos": "D"}, {"team_id": 84, "player_id": 22170, "player": "Pierre Lees Melou", "number": 8, "pos": "M"}, {"team_id": 84, "player_id": 22174, "player": "Adrien Tameze", "number": 5, "pos": "M"}, {"team_id": 84, "player_id": 22167, "player": "Danilo", "number": 21, "pos": "M"}, {"team_id": 84, "player_id": 3165, "player": "Youcef Atal", "number": 20, "pos": "M"}, {"team_id": 84, "player_id": 22173, "player": "Allan Saint-Maximin", "number": 7, "pos": "F"}, {"team_id": 84, "player_id": 22177, "player": "Igniatius Ganago", "number": 14, "pos": "F"}], "substitutes": [{"team_id": 84, "player_id": 22159, "player": "Yannis Clementia", "number": 16, "pos": "G"}, {"team_id": 84, "player_id": 22160, "player": "Olivier Boscagli", "number": 28, "pos": "D"}, {"team_id": 84, "player_id": 22171, "player": "Jean-Victor Makengo", "number": 27, "pos": "M"}, {"team_id": 84, "player_id": 22172, "player": "Ihsan Sacko", "number": 18, "pos": "M"}, {"team_id": 84, "player_id": 2959, "player": "Bassem Srarfi", "number": 11, "pos": "F"}, {"team_id": 84, "player_id": 22178, "player": "Micka\u00ebl Le Bihan", "number": 10, "pos": "F"}, {"team_id": 84, "player_id": 22176, "player": "Assil Jaziri", "number": 33, "pos": "SUB"}]}}}}

我想剪切此json数据并将每个球队名称及其阵型数据存储到我的数据库中。但是问题在于API提供程序使用团队名称 Paris Saint Germain Nice 来命名密钥,因为它,由于json数据没有固定密钥,所以我无法在此json上循环名称。我如何为两支球队提取编队密钥

1 个答案:

答案 0 :(得分:0)

解析JSON后,您将拥有一个词典字典,可以在其中使用普通的词典方法,包括keys

>>> import json
>>> data = json.loads(' {"api": {"results": 2, "lineUps": {"Paris Saint Germain": [...]')
>>> lineups = data['api']['lineUps']
>>> for team in lineups.keys():
...   print(f'Team Name: {team}')
...   print(f'Formation: {lineups[team]["formation"]}')
... 
Team Name: Paris Saint Germain
Formation: 4-2-3-1
Team Name: Nice
Formation: 4-3-3