Python 和 JSON - TypeError 列表索引必须是整数而不是 str

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24708634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:02:32  来源:igfitidea点击:

Python and JSON - TypeError list indices must be integers not str

pythonjson

提问by user3718365

I am learning to use Python and APIs (specifically, this World Cup API, http://www.kimonolabs.com/worldcup/explorer)

我正在学习使用 Python 和 API(特别是这个世界杯 API,http://www.kimonolabs.com/worldcup/explorer

The JSON data looks like this:

JSON 数据如下所示:

[
  {
    "firstName": "Nicolas Alexis Julio",
    "lastName": "N'Koulou N'Doubena",
    "nickname": "N. N'Koulou",
    "assists": 0,
    "clubId": "5AF524A1-830C-4D75-8C54-2D0BA1F9BE33",
    "teamId": "DF25ABB8-37EB-4C2A-8B6C-BDA53BF5A74D",
    "id": "D9AD1E6D-4253-4B88-BB78-0F43E02AF016",
    "type": "Player"
  },

 {
    "firstName": "Alexandre Dimitri",
    "lastName": "Song-Billong",
    "nickname": "A. Song",
    "clubId": "35BCEEAF-37D3-4685-83C4-DDCA504E0653",
    "teamId": "DF25ABB8-37EB-4C2A-8B6C-BDA53BF5A74D",
    "id": "A84540B7-37B6-416F-8C4D-8EAD55D113D9",
    "type": "Player"
  },
   ]

I am simply trying to print all of the firstNames in this API. Here's what I have:

我只是想打印这个 API 中的所有 firstNames。这是我所拥有的:

import urllib2
import json

url = "http://worldcup.kimonolabs.com/api/players?apikey=xxx"
json_obj = urllib2.urlopen(url).read
readable_json = json.dumps(json_obj)
playerstuff = readable_json['firstName']
for i in playerstuff:
    print i['firstName']

But when I run it, I get the error "...line 8, in ...TypeError: list indices must be integers, not str"

但是当我运行它时,我收到错误“...第 8 行,...TypeError:列表索引必须是整数,而不是 str”

I have looked around for solutions, but seem to find questions to more "in depth" API questions and I don't really understand it all yet, so any help or explanation as to what I need to do would be amazing. Thank you!

我四处寻找解决方案,但似乎找到了更“深入”的 API 问题的问题,但我还没有真正理解这一切,因此任何有关我需要做的事情的帮助或解释都会很棒。谢谢!

采纳答案by jwodder

First of all, you should be using json.loads, not json.dumps. loadsconverts JSON source text to a Python value, while dumpsgoes the other way.

首先,您应该使用json.loads,而不是json.dumpsloads将 JSON 源文本转换为 Python 值,dumps反之亦然。

After you fix that, based on the JSON snippet at the top of your question, readable_jsonwill be a list, and so readable_json['firstName']is meaningless. The correct way to get the 'firstName'field of every element of a list is to eliminate the playerstuff = readable_json['firstName']line and change for i in playerstuff:to for i in readable_json:.

在您修复该问题后,根据您问题顶部的 JSON 片段,readable_json将是一个列表,因此readable_json['firstName']毫无意义。获取'firstName'列表中每个元素的字段的正确方法是消除该playerstuff = readable_json['firstName']行并更改for i in playerstuff:for i in readable_json:.

回答by AlexLordThorsen

You can simplify your code down to

您可以将代码简化为

url = "http://worldcup.kimonolabs.com/api/players?apikey=xxx"
json_obj = urllib2.urlopen(url).read
player_json_list = json.loads(json_obj)
for player in readable_json_list:
    print player['firstName']

You were trying to access a list element using dictionary syntax. the equivalent of

您试图使用字典语法访问列表元素。相当于

foo = [1, 2, 3, 4]
foo["1"]

It can be confusing when you have lists of dictionaries and keeping the nesting in order.

当您有字典列表并保持嵌套顺序时,这可能会令人困惑。

回答by Hernan Ramirez

I solved changing

我解决了改变

readable_json['firstName']

by

经过

readable_json[0]['firstName']