2024-05-15 02:02:08 发布
网友
我还是一个初学者,刚刚开始使用Python。 我试图通过JSON获取Riot Games(只有EUW)API的玩家的等级和等级,但我得到了一个例外:
print (responseJSON2[ID][0]['tier']) TypeError: list indices must be integers or slices, not str
我不知道我要改变什么,也许有人可以帮我:) 代码:
responseJSON2是list。列表有索引(0,1,2,…)。在
responseJSON2
list
列表需要使用int:
ID = str(ID)
是错的,你需要有个智力测验!在
试试看
你可以转换回字符串:
def requestRankedData(ID, APIKey): URL= "https://euw1.api.riotgames.com/lol/league/v3/positions/by-summoner/"+str(ID)+"?api_key="+APIKey print (URL) response = requests.get(URL) return response.json()
您需要在响应中找到与您的ID匹配的索引:
responseJSON2 = requestRankedData(ID, APIKey) ID_idx = responseJSON2.index(str(ID)) print (responseJSON2[ID_idx][0]['tier']) print (responseJSON2[ID_idx][0]['entries'][0]['rank']) print (responseJSON2[ID_idx][0]['entries'][0]['leaguePoints'])
responseJSON2
是list
。列表有索引(0,1,2,…)。在列表需要使用int:
是错的,你需要有个智力测验!在
试试看
^{pr2}$你可以转换回字符串:
您需要在响应中找到与您的ID匹配的索引:
相关问题 更多 >
编程相关推荐