Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:解析json时,列表索引必须是整数,而不是str_Python_Json - Fatal编程技术网

Python TypeError:解析json时,列表索引必须是整数,而不是str

Python TypeError:解析json时,列表索引必须是整数,而不是str,python,json,Python,Json,提交请求后,我收到了以下json回复: {"type": [ {"ID": "all", "count": 1, "references": [ { "id": "Boston,MA,02118", "text": "Boston,MA,02118", "val": "Boston,MA,02118", "type": 1 ,"zip": "02118","city": "Boston","state": "MA","lt": "42.3369","lg": "-71.0637","s"

提交请求后,我收到了以下json回复:

{"type": [
{"ID": "all", "count": 1, "references": [
    { "id": "Boston,MA,02118", "text": "Boston,MA,02118", "val": "Boston,MA,02118", "type": 1 ,"zip": "02118","city": "Boston","state": "MA","lt": "42.3369","lg": "-71.0637","s": ""}
] }
] } 
我在变量j中捕获响应,并按如下方式加载它

l = json.loads(j)
现在我有:

>>> type(l)
<type 'dict'>
>>> l['type']['references']
Traceback (most recent call last):
  File "C:\PyCharm\helpers\pydev\pydevd_exec.py", line 3, in Exec
    exec exp in global_vars, local_vars
  File "<input>", line 1, in <module>
TypeError: list indices must be integers, not str
>>类型(l)
>>>l['type']['references']
回溯(最近一次呼叫最后一次):
文件“C:\PyCharm\helpers\pydev\pydev_exec.py”,第3行,在exec中
全局变量、本地变量中的exec exp
文件“”,第1行,在
TypeError:列表索引必须是整数,而不是str

我做错了什么

类型的值是一个列表。尝试执行
键入(l['type'])
以确认

要访问该值,您需要使用:

l['type'][0]['references']


这将为您提供另一个列表。

l['type']
为您提供一个列表,而不是对象。所以你必须像访问列表一样访问它

l['type'][0]["references"]