简体   繁体   English

使用 JSON 获取 object 的所有值

[英]Get all values of object with JSON

I wouldlike to get all summonername and leaguePoints from a JSON file in python.我想从 python 中的 JSON 文件中获取所有的召唤师姓名和联赛积分。

Json file: Json 文件:

{
        "queueType": "RANKED_SOLO_5x5",
        "summonerName": "TheUnshackIedone",
        "hotStreak": true,
        "wins": 716,
        "veteran": true,
        "losses": 587,
        "rank": "I",
        "tier": "CHALLENGER",
        "inactive": false,
        "freshBlood": false,
        "leagueId": "65ebcd4f-368c-30f6-a635-976beb0e1a8c",
        "summonerId": "D4tAkhItmCDY6R8XsZXGtVtgukEq8MAONe1mlSDOL4CQxQQ",
        "leaguePoints": 1436
    },
    {
        "queueType": "RANKED_SOLO_5x5",
        "summonerName": "waoedsjad",
        "hotStreak": false,
        "wins": 352,
        "veteran": true,
        "losses": 197,
        "rank": "I",
        "tier": "CHALLENGER",
        "inactive": false,
        "freshBlood": false,
        "leagueId": "65ebcd4f-368c-30f6-a635-976beb0e1a8c",
        "summonerId": "SwMmXPPk4VV1ThMOm5vZZDJnkuPyiP2S37iPfAHHZ6_5G5yp",
        "leaguePoints": 1389
    },
    {
        "queueType": "RANKED_SOLO_5x5",
        "summonerName": "FNC Bwipo",
        "hotStreak": false,
        "wins": 537,
        "veteran": true,
        "losses": 387,
        "rank": "I",
        "tier": "CHALLENGER",
        "inactive": false,
        "freshBlood": false,
        "leagueId": "65ebcd4f-368c-30f6-a635-976beb0e1a8c",
        "summonerId": "TpMdwXDCPmCAZgUtI1cjoXSL6WFUe9QFaIiTLtbivnC8RPs",
        "leaguePoints": 1323
    },

I start to do this but I don't understand how to get all data like that:我开始这样做,但我不明白如何获取所有这样的数据:

TheUnshackIedone 1436 The UnshackIedone 1436

waoedsjad 1389第1389章

FNC Bwipo 1323 FNC Bwipo 1323

Here example for 3 pseudos but if I have 100, 1000 or more?这里是 3 个伪的示例,但如果我有 100、1000 或更多? Do I need to use loop?我需要使用循环吗? or it exist another way?还是以另一种方式存在?

My python file:我的 python 文件:

def ClassementRank():
    responseJSON = requestRank()

    print(responseJSON[0]['summonerName'])
    print(responseJSON[0]['tier'])
    print(responseJSON[0]['leaguePoints'], "lp")

Thansk for any help !感谢您的帮助!

Use a for loop :使用for loop

def ClassementRank():
    responseJSON = requestRank()
    for entry in responseJSON:
        print(entry['summonerName'])
        print(entry['tier'])
        print(entry['leaguePoints'])

You can use for loop and store it in a list by:您可以使用 for 循环并将其存储在列表中:

    contents = [{
        "queueType": "RANKED_SOLO_5x5",
        "summonerName": "TheUnshackIedone",
        "hotStreak": True,
        "wins": 716,
        "veteran": True,
        "losses": 587,
        "rank": "I",
        "tier": "CHALLENGER",
        "inactive": False,
        "freshBlood": False,
        "leagueId": "65ebcd4f-368c-30f6-a635-976beb0e1a8c",
        "summonerId": "D4tAkhItmCDY6R8XsZXGtVtgukEq8MAONe1mlSDOL4CQxQQ",
        "leaguePoints": 1436
    },
    {
        "queueType": "RANKED_SOLO_5x5",
        "summonerName": "waoedsjad",
        "hotStreak": False,
        "wins": 352,
        "veteran": True,
        "losses": 197,
        "rank": "I",
        "tier": "CHALLENGER",
        "inactive": False,
        "freshBlood": False,
        "leagueId": "65ebcd4f-368c-30f6-a635-976beb0e1a8c",
        "summonerId": "SwMmXPPk4VV1ThMOm5vZZDJnkuPyiP2S37iPfAHHZ6_5G5yp",
        "leaguePoints": 1389
    },
    {
        "queueType": "RANKED_SOLO_5x5",
        "summonerName": "FNC Bwipo",
        "hotStreak": False,
        "wins": 537,
        "veteran": True,
        "losses": 387,
        "rank": "I",
        "tier": "CHALLENGER",
        "inactive": False,
        "freshBlood": False,
        "leagueId": "65ebcd4f-368c-30f6-a635-976beb0e1a8c",
        "summonerId": "TpMdwXDCPmCAZgUtI1cjoXSL6WFUe9QFaIiTLtbivnC8RPs",
        "leaguePoints": 1323
    }]

li = [{item.get('summonerName'),item.get('leaguePoints')} for item in contents]
print(li)

Here is working screenshot:这是工作截图:

工作演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM