Python TypeError:解析json时,列表索引必须是整数,而不是str
提交请求后,我收到了以下json回复: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"
{"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"]