给出json.decoder.jsondeCoderror的有效json:应为“,”分隔符

2024-04-20 13:32:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习如何使用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:\Users\Tanishq\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "C:\Users\Tanishq\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\Tanishq\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 6 column 21 (char 71)

Tags: inpyjsonstringlocallinepeopleusers
2条回答

您的错误在json文本字符串中,您错误地定义了对phone

你应该这样做

....
    "phone": "666-625-7263",
....

我的意思是,数字必须介于“”之间,因为它是字符串,而不是数字(因为-符号)

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)

相关问题 更多 >