【问题标题】:How to parse this code?如何解析这段代码?
【发布时间】:2016-02-13 17:28:58
【问题描述】:

从今天早上开始,我一直在思考如何从 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

【问题讨论】:

  • ids 不是字典,而是字符串。
  • 尝试使用 JSON 查看器/美化器,例如 codebeautify.org/jsonviewer 来帮助您可视化 JSON 中的结构。

标签: python json parsing web python-requests


【解决方案1】:

您正在寻找以下内容:

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

【讨论】:

    【解决方案2】:

    您正在尝试访问ids,就好像它是dictionary,但您得到的TypeError 却说它是String

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-31
      • 2011-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-20
      相关资源
      最近更新 更多