所以我在这里难倒......我试图从API过滤JSON结果,并且在找到嵌套的" playerID"在下面,输出整个JSON块直到"键入"
JSON
{
"included": [
{
"type": "participant",
"id": "efb753a7-bc6b-4cf0-925a-df4693b51690",
"attributes": {
"actor": "",
"shardId": "pc-na",
"stats": {
"DBNOs": 0,
"assists": 0,
"boosts": 0,
"damageDealt": 21.78,
"deathType": "byplayer",
"headshotKills": 0,
"heals": 1,
"killPlace": 69,
"killPoints": 1204,
"killPointsDelta": -12.1259718,
"killStreaks": 0,
"kills": 0,
"lastKillPoints": 0,
"lastWinPoints": 0,
"longestKill": 0,
"mostDamage": 0,
"name": "AshTrayyyyyyyy",
"playerId": "account.a6e705568e1e490189d59606892765a9",
"revives": 0,
"rideDistance": 0,
"roadKills": 0,
"teamKills": 0,
"timeSurvived": 293,
"vehicleDestroys": 0,
"walkDistance": 197.17308,
"weaponsAcquired": 0,
"winPlace": 28,
"winPoints": 1152,
"winPointsDelta": -4.64524174
}
}
},
{
"type": "participant",
"id": "e916ce2e-aea4-4806-9aa1-449113df793c",
"attributes": {
"actor": "",
"shardId": "pc-na",
"stats": {
"DBNOs": 0,
"assists": 0,
"boosts": 1,
"damageDealt": 54.3429337,
"deathType": "byplayer",
"headshotKills": 0,
"heals": 7,
"killPlace": 56,
"killPoints": 1185,
"killPointsDelta": -5.10656166,
"killStreaks": 0,
"kills": 0,
"lastKillPoints": 0,
"lastWinPoints": 0,
"longestKill": 0,
"mostDamage": 0,
"name": "PanzerCheneyJr",
"playerId": "account.380f694417594236a8dc9f5dcc0b0ed5",
"revives": 0,
"rideDistance": 1869.12939,
"roadKills": 0,
"teamKills": 0,
"timeSurvived": 1476,
"vehicleDestroys": 0,
"walkDistance": 2950.75659,
"weaponsAcquired": 0,
"winPlace": 10,
"winPoints": 1184,
"winPointsDelta": 15.2799187
}
}
},
{
"type": "roster",
"id": "f876ef81-bd51-494b-9cf0-49d4da725ff0",
"attributes": {
"shardId": "pc-na",
"stats": {
"rank": 40,
"teamId": 11
},
"won": "false"
},
"relationships": {
"participants": {
"data": [
{
"type": "participant",
"id": "0c936615-eb39-42f8-bbe3-5b17bcf6c7dd"
},
{
"type": "participant",
"id": "62a6ed31-f5bf-4b0a-b867-8260774dcb90"
}
]
},
"team": {
"data": null
}
}
},
{
"type": "participant",
"id": "8b63d4d9-a673-48d8-9e56-cfe2109044d8",
"attributes": {
"actor": "",
"shardId": "pc-na",
"stats": {
"DBNOs": 1,
"assists": 1,
"boosts": 5,
"damageDealt": 466.477539,
"deathType": "byplayer",
"headshotKills": 0,
"heals": 4,
"killPlace": 3,
"killPoints": 1105,
"killPointsDelta": 54.3647232,
"killStreaks": 0,
"kills": 4,
"lastKillPoints": 0,
"lastWinPoints": 0,
"longestKill": 74,
"mostDamage": 0,
"name": "JustFallen",
"playerId": "account.1ea2035542e44f37bb2b7a3679a91fcc",
"revives": 0,
"rideDistance": 1945.68457,
"roadKills": 0,
"teamKills": 0,
"timeSurvived": 1561,
"vehicleDestroys": 0,
"walkDistance": 2272.8418,
"weaponsAcquired": 0,
"winPlace": 4,
"winPoints": 1179,
"winPointsDelta": 28.698019
}
}
}
}
对于Python而言,这是我完全难倒的地方。我要做的是返回整个参与者块,并将所有内容嵌套在其中。但是我需要通过" playerID"进行过滤。在属性下 - >统计资料。
这是我尝试过的Python ......
的Python
included = (data['included'])
included = json.dumps(included)
input_dict = json.loads(included)
print(playerid)
for d in input_dict:
for key, value in d.iteritems():
print (key, value)
output_dict = [x for x in input_dict if x['playerID'] == playerid]
output_json = json.dumps(output_dict)
print(output_json)
非常感谢任何帮助。
答案 0 :(得分:0)
您似乎很清楚如何转储和加载JSON,因此以下是过滤器的部分:
playerid = 'account.380f694417594236a8dc9f5dcc0b0ed5'
new_data = [x
for x in data['included']
if x['type'] == 'participant' and
x['attributes']['stats']['playerId'] == playerid]