简体   繁体   中英

Python .json() moving first item to end?

Response data:

{
  "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
    }
  ]
}

When running it through .json() in the code below I get the "total" at the end of the output, which I'm thinking is what breaks my code to iterate through it. Python2 code:

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

I get:

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

JSON Output:

{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}

Should I not be using .json() on the requests data? Seems to be an easier way of iterating through the data and setting it to variables.

This has nothing to do with the order of the keys in the output; both Python dictionaries and JSON objects are unordered , meaning that you cannot rely on the order of the keys in the data structure.

You are looping over the dictionary, so i is each of the keys. It'll be bound to 'total' and 'rankings' , not to dictionaries. Since strings are sequences you can only use integers to index them, hence your error message; 'total'['total'] makes little sense.

Don't loop over the outer object. Use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM