繁体   English   中英

有效 json 给出 json.decoder.JSONDecodeError: Expecting ',' delimiter

[英]Valid json giving json.decoder.JSONDecodeError: Expecting ',' delimiter

我正在学习如何使用 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)

您的错误在 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM