【发布时间】:2021-04-19 23:24:13
【问题描述】:
Python 代码:
import json
aaa='''
{
"eee":"yes",
"something": null,
"ok": ["no","mmm","eee"],
"please":false,
"no": {"f":true,"h":"ttt"}
}
'''
data=json.loads(aaa)
当我这样做时:
print(len(data))
我得到了预期:
5
和
print(len(data['ok']))
给了
3
但不知何故
print(data[0])
给予
Traceback (most recent call last):
File "file.py", line 34, in <module>
print(data[0])
KeyError: 0
如何使用它的索引在这个 JSON 对象中获取一个术语?
【问题讨论】:
-
索引需要一个列表。
-
获得
KeyError的原因是因为您的dictionary中没有0作为密钥