如何解析这段代码?

时间:2016-02-13 17:28:59

标签: python json parsing web python-requests

我从今天早上起就被困在了如何获得“Tier'来自here的这个json答案。我一直在尝试这个:

url = #given
response = requests.get(url)
data = json.loads(response.text)

for ids in data:
    print(ids['tier'])

它一直告诉我:print(ids['tier']) TypeError: string indices must be integers

2 个答案:

答案 0 :(得分:4)

您正在寻找以下内容:

import requests
import json

url = 'https://euw.api.pvp.net/api/lol/euw/v2.5/league/by-summoner/266302,29338851,44112476,24693443,42900539,32556822,25575027,27677126,24648065,19083702/entry?api_key=2c0543ab-aeca-476d-8276-a5c23748898c'
response = requests.get(url)
data = json.loads(response.text)

for ids in data:
    print(data[ids][0]['tier'])

<强>输出

PLATINUM
SILVER
SILVER

答案 1 :(得分:0)

您正试图访问typedef struct { int Member1; int Member2; } Data; typedef struct { Data TheData; uint8_t Reserved[512-sizeof(Data)]; } FullData; ,就好像它是ids一样,但您获得的dictionary表示它是TypeError