简体   繁体   English

Python-检查值是否等于返回的json响应

[英]Python - Check if value is equal to returned json response

Tell me if I'm wording this poorly. 告诉我我的措辞不好。 I'm calling a json response from the League of Legends API. 我正在从英雄联盟API调用json响应。 I get back a response that looks like this. 我得到的回复看起来像这样。

{
"champions": [
{
    "id": 111,
    "stats": {
        "maxChampionsKilled": 2,
        "maxNumDeaths": 5,
        "mostChampionKillsPerSession": 2,
        "mostSpellsCast": 0,
        "totalAssists": 25,
        "totalChampionKills": 2,
        "totalDamageDealt": 40838,
        "totalDamageTaken": 27900,
        "totalDeathsPerSession": 5,
        "totalDoubleKills": 0,
        "totalFirstBlood": 0,
        "totalGoldEarned": 11070,
        "totalMagicDamageDealt": 21083,
        "totalMinionKills": 56,
        "totalPentaKills": 0,
        "totalPhysicalDamageDealt": 12876,
        "totalQuadraKills": 0,
        "totalSessionsLost": 1,
        "totalSessionsPlayed": 1,
        "totalSessionsWon": 0,
        "totalTripleKills": 0,
        "totalTurretsKilled": 1,
        "totalUnrealKills": 0
    }
}, 

Currently I have a list like so: champ_list[] and I have all the id's (in this case 111) in this list. 目前,我有一个这样的列表:champ_list []并且此列表中包含所有ID(在本例中为111)。 So completed it would be something like champ_list[111,67,4,23], and it can keep growing up to the number of champions in the game. 如此完成后,将类似于champ_list [111,67,4,23],并且可以不断增加游戏中的冠军数量。

I then do the following: 然后,我执行以下操作:

if '111' in champ_list:
    print("Nautilus")

Here's the question - if the above statement is true and Nautilus is printed how do I get {stats},{totalSessionsPlayed} to print its value? 问题是-如果上面的陈述正确,并且打印了Nautilus,如何获取{stats},{totalSessionsPlayed}来打印其值? I realized I could add 我意识到我可以补充

print(['champions'][0]['stats']['totalSessionsPlayed'] 

to the end of my statement above, however if the return data doesn't stay in the same order I'm boned. 到上面我的声明的结尾,但是,如果返回数据的排列顺序不一致,我就会陷入困境。 Also, if the champ_list becomes very long, it would be a pain to hardcode each line. 另外,如果champ_list变得很长,则对每行进行硬编码将很痛苦。 Is there a way to do this "better" or more consistent? 有没有办法做到“更好”或更一致? Sorry if I'm reaching here, but any help is greatly appreciated! 抱歉,如果我要到达这里,我们将不胜感激!

Your JSON is indirectly storing an association list that maps 111 to the appropriate stat block. 您的JSON间接存储了一个关联列表,该列表将111映射到适当的stat块。 You should first transform this into a proper Python mapping (ie, a dict ): 您应该首先将其转换为适当的Python映射(即dict ):

json_data = { ... }
champions = {}
for d in json_data['champions']:
    champions[d['id']] = d['stats']

Now given champ_id = '111' , you can simply write 现在给定champ_id = '111' ,您可以简单地编写

print(champions[champ_id]['totalSessionsPlayed'])

you can filter the result list for the id specified and pick the first element if it is found. 您可以过滤指定ID的结果列表,并选择找到的第一个元素。

nautilus_id = 111
try:
    nautilus = filter(lambda c: c['id'] == nautilus_id, champ_list)[0]
except:
    nautilus = None

from there on you can access all data in nautilus: 从那里,您可以访问nautilus中的所有数据:

print nautilus['stats']

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

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