张图南 发表于 2020-11-22 22:46:32

使用json.loads时报错

写了一个json数据,转换成python格式时报错了。
import json
obj = """
{"name":"Wes",
"places_lived":["United States","Spain","Germany"],
"pet":"bull",
"siblings":[{"name":'Scott',"age":30,"pets":["Zeus","Zuko"]},
             {"name":"Katie","age":38,"pets":["Sixes","Stache","Cisco"]}]
}
"""
result = json.loads(obj)

报错代码:
Traceback (most recent call last):
File "11_22.py", line 10, in <module>
    result = json.loads(obj)
File "C:\Users\asus\AppData\Local\Programs\Python\Python38\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)
File "C:\Users\asus\AppData\Local\Programs\Python\Python38\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\asus\AppData\Local\Programs\Python\Python38\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 5 column 22 (char 105)

疾风怪盗 发表于 2020-11-22 23:16:20

注意标点符号,json里面要求都是双引号"name":'Scott'
import json

obj = """
{"name":"Wes",
"places_lived":["United States","Spain","Germany"],
"pet":"bull",
"siblings":[{"name":"Scott","age":30,"pets":["Zeus","Zuko"]},
             {"name":"Katie","age":38,"pets":["Sixes","Stache","Cisco"]}]
}
"""
result = json.loads(obj)
print(result)
页: [1]
查看完整版本: 使用json.loads时报错