鱼C论坛

 找回密码
 立即注册
查看: 1580|回复: 1

[已解决]使用json.loads时报错

[复制链接]
发表于 2020-11-22 22:46:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
写了一个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)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 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)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-17 14:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表