python - Find Value From JSON -
first off, i'm sorry title, didn't know how specific issue there.
ok, i'm doing league of legends api. data displayed json , can found here, want loop through it. have data script displays stats each player on match. in stats there id champion being played. want use id , compare each key json file above , if finds matching key - id can name of champion. problem don't know how can iterate through each 1 of them, because can see on file there this (posted image because couldn't indent right.
how can access id of each one? because, again, can see, each 1 has different names (jax, sona, tristana, etc...) want key , name of each one. tried using loop couldn't list first 1 i'm pretty lost time. :)
code:
champ_r = requests.get("https://global.api.pvp.net/api/lol/static-data/lan/v1.2/champion?" "api_key=").json() x in champ_r['data'][0]: pprint(x['id'])
if run keyerror: 0
, if remove typeerror: string indices must integers
as output return ids , names of each 1 of champions. like:
aatrox - key ahri - key...
... , on.
based on sample output trying get, "key" of dictionary name looking for, , value associated key dictionary. looking "key" key in dictionary. so, when iterating on dictionary, value dictionary. can make iteration easy on you, using items method in iteration.
furthermore, data stored in "data" key big json response. so, code should this:
for key, value in champ_r['data'].items(): print(key, value['key'])
sample output of first few lines:
sona 37 draven 119 fiddlesticks 9 volibear 106 pantheon 80 singed 27 vladimir 8 ekko 245
Comments
Post a Comment