首页 > 解决方案 > 给出 json.decoder.JSONDecodeError 的有效 json: 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)

标签: pythonjson

解决方案


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

你应该做

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

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


推荐阅读