Python .json()将第一个项目移动到结尾?

时间:2015-04-12 01:01:49

标签: python json python-requests

回复数据:

{
  "total": 488,
  "rankings": [
    {
      "name": "ABC",
      "class": 8,
      "spec": 1,
      "total": 48801.38671875,
      "duration": 270381,
      "startTime": 1428524959504,
      "fightID": 34,
      "reportID": "9BwMKrNz1VamGQkq",
      "guild": "ABC",
      "server": "ABC",
      "itemLevel": 700
    }
  ]
}

当在下面的代码中通过.json()运行它时,我在输出的末尾得到“total”,我正在考虑是什么破坏了我的代码来迭代它。 Python2代码:

def wow_rankings():
    response = requests.get("https://www.warcraftlogs.com:443/v1/rankings/encounter/1691?metric=dps&size=20&difficulty=5&region=1&class=8&spec=1&bracket=0&limit=3&page=1&api_key=abc")

    json_data = response.json()
    print json_data #shows the "total" item at the end of the output not front.
    for i in json_data:
        rank_totals = i['total']
        for x in i['rankings']:
            rank_name = x['name']
            rank_class = x['class']
            rank_spec = x['spec']
            rank_total = x['total']
            rank_duration = x['startTime']
            rank_fightID = x['fightID']
            rank_reportID = x['reportID']
            rank_guild = x['guild']
            rank_server = x['server']
            rank_ilevel = x['itemLevel']
            print rank_totals
            print rank_name, rank_class, rank_total, rank_fightID, rank_guild, rank_server, rank_ilevel

我明白了:

for x in i['rankings']:
TypeError: string indices must be integers

JSON输出:

{u'rankings': [{u'guild': u'BIZZNO', u'name': u'Kecks', u'fightID': 34, u'itemLevel': 700, u'server': u'Ra
vencrest', u'class': 8, u'reportID': u'9BwMKrNz1VamGQkq', u'startTime': 1428524959504, u'duration': 270381
, u'total': 48801.38671875, u'spec': 1}, {u'guild': u'\u0424\u044c\u044e\u0436\u043d', u'name': u'\u0422\u
044d\u0439\u043a\u0430\u0445\u0445', u'fightID': 8, u'itemLevel': 700, u'server': u'\u0421\u0432\u0435\u04
36\u0435\u0432\u0430\u0442\u0435\u043b\u044c \u0414\u0443\u0448', u'class': 8, u'reportID': u'Lndmj1NzRVwv
tMTg', u'startTime': 1427907142553, u'duration': 280126, u'total': 47983.8828125, u'spec': 1}, {u'guild': 
u'Encore', u'name': u'Yazuki', u'fightID': 14, u'itemLevel': 699, u'server': u'Illidan', u'class': 8, u're
portID': u'z4MC2yjYfBgHNxw1', u'startTime': 1427854657905, u'duration': 266207, u'total': 47540.2109375, u
'spec': 1}], u'total': 488}

我是否应该在请求数据上使用.json()?似乎是一种迭代数据并将其设置为变量的简单方法。

1 个答案:

答案 0 :(得分:2)

这与输出中键的顺序无关; Python字典和JSON对象都是无序,这意味着您不能依赖数据结构中键的顺序。

您正在遍历字典,因此i是每个键。它将绑定到'total''rankings',而不是字典。由于字符串是序列,因此您只能使用整数来索引它们,因此您的错误消息; 'total'['total']毫无意义。

不要在外部物体上循环。使用:

rank_totals = json_data['total']
print rank_totals
for x in json_data['rankings']:
    rank_name = x['name']
    rank_class = x['class']
    rank_spec = x['spec']
    rank_total = x['total']
    rank_duration = x['startTime']
    rank_fightID = x['fightID']
    rank_reportID = x['reportID']
    rank_guild = x['guild']
    rank_server = x['server']
    rank_ilevel = x['itemLevel']
    print rank_name, rank_class, rank_total, rank_fightID, rank_guild, rank_server, rank_ilevel