在词典中导航

时间:2016-02-14 14:27:49

标签: python json parsing dictionary navigation

我一直在浏览从here加载json生成的字典。有谁可以帮助我吗。谢谢你。 我试过tier_d[test - 1] = data[summoners[test - 1]['entries']['leaguePoints']],但它告诉我expected integers。我想要获取联盟积分'从它。

Summoners_id是另一个请求提供的ID,它们看起来像' 24657246'例如

url = 'https://{0}.api.pvp.net/api/lol/{0}/v2.5/league/by-summoner/{1},{2},{3},{4},{5},{6},{7},{8},{9},{10}/entry?api_key={11}'.format(region, summoner_id_1, summoner_id_2, summoner_id_3, summoner_id_4, summoner_id_5, summoner_id_6, summoner_id_7, summoner_id_8, summoner_id_9, summoner_id_10, key)
print(url)
response = requests.get(url)
data = json.loads(response.text)
tier = ['Unranked', 'Unranked', 'Unranked', 'Unranked', 'Unranked', 'Unranked', 'Unranked', 'Unranked', 'Unranked', 'Unranked']
tier_d = ['', '', '', '', '', '', '', '', '', '']

done = False
test = 0

rankeds = []

for ids in data:
    rankeds.append(ids)

    print(rankeds)
print(summoners)

while test <= 9:
    test += 1
    if summoners[test - 1] in rankeds:
        tier_d[test - 1] = data[summoners[test - 1]['entries']['leaguePoints']]
        tier[test - 1] = data[summoners[test - 1]][0]['tier']
        print(data[summoners[test - 1]][0]['tier'])

    print(tier)

1 个答案:

答案 0 :(得分:2)

您有一些listsdictionaries相互嵌套。你必须仔细检查它们。

data[summoners[test - 1]]的输出是:

[{'entries': [{'division': 'I',
    'isFreshBlood': False,
    'isHotStreak': False,
    'isInactive': False,
    'isVeteran': False,
    'leaguePoints': 0,
    'losses': 14,
    'playerOrTeamId': 'xxyyzz',
    'playerOrTeamName': 'xxyyzz',
    'wins': 8}],
  'name': "Talon's Army",
  'queue': 'xxyyzz',
  'tier': 'BRONZE'},
 {'entries': [{'division': 'V',
    'isFreshBlood': False,
    'isHotStreak': False,
    'isInactive': False,
    'isVeteran': False,
    'leaguePoints': 18,
    'losses': 2,
    'playerOrTeamId': 'xxyyzz',
    'playerOrTeamName': 'xxyyzz',
    'wins': 5}],
  'name': "xxyyzz",
  'queue': 'xxyyzz',
  'tier': 'SILVER'}]

所以这应该给你leaguePoints值:

tier_d[test - 1] = data[summoners[test - 1]][0]['entries'][0]['leaguePoints']
tier_d[test - 1] = data[summoners[test - 1]][1]['entries'][0]['leaguePoints']

取决于列表中的第一个或第二个条目。