我正在学习如何使用python处理json,但它给了我一个错误。
这是我的代码
import json
people_string = """
{
"people": [
{
"name": "John Smith",
"phone": 666-625-7263,
"emails": ["john.smith@fakemail.com","johnsmith@workmail.com"],
"has_license": false
},
{
"name": "Jane Doe",
"phone": 666-625-7263,
"emails": null,
"has_license": true
}
]
}
"""
data = json.loads(people_string)
我得到以下错误:
Traceback (most recent call last):
File "c:/Users/Tanishq/Desktop/Tanishq-imp/python tutorials/json-35.py", line 20, in <module>
data = json.loads(people_string) #https://docs.python.org/3/library/json.html#encoders-and-decoders
File "C:UsersTanishqAppDataLocalProgramsPythonPython38-32libjson__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "C:UsersTanishqAppDataLocalProgramsPythonPython38-32libjsondecoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:UsersTanishqAppDataLocalProgramsPythonPython38-32libjsondecoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 6 column 21 (char 71)
用double quote
(" "
(包装phone
值
import json
people_string = '''
{
"people": [
{
"name": "John Smith",
"phone": "666-625-7263",
"emails": ["john.smith@fakemail.com","johnsmith@workmail.com"],
"has_license": false
},
{
"name": "Jane Doe",
"phone": "666-625-7263",
"emails": null,
"has_license": true
}
]
}'''
data = json.loads(people_string)
您的错误在json文本字符串中,您错误地定义了对phone
。。。
你应该做
....
"phone": "666-625-7263",
....
我的意思是,这个数字必须在"因为它是字符串,而不是数字(因为-符号(