json.loads(string) string中的key必须使用双引号,不能使用单引号

实验验证:

我是经过重重实验,才知道,json.loads(string)的string中的key必须使用双引号的。

json_txt = "{'doors': [[{'pointDown': {'x': 3307.0001220703125, 'y': 2367.75}, 'pointUp': {'x': 3307.0001220703125, 'y': 408.0}, 'weight': 0.912015974521637, 'detect_bottom': 1, 'detect_door': 0, 'balconydoor': 0}, {'pointDown': {'x': 4672.0001220703125, 'y': 2331.0}, 'pointUp': {'x': 4672.0001220703125, 'y': 408.0}, 'weight': 0.912015974521637, 'detect_bottom': 1, 'detect_door': 0, 'balconydoor': 0}]] }"
json.loads(json_txt)

就会报如下错误:

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 14 (char 13)

但是把里面的单引号替换为双引号:

json_txt = "{'doors': [[{'pointDown': {'x': 3307.0001220703125, 'y': 2367.75}, 'pointUp': {'x': 3307.0001220703125, 'y': 408.0}, 'weight': 0.912015974521637, 'detect_bottom': 1, 'detect_door': 0, 'balconydoor': 0}, {'pointDown': {'x': 4672.0001220703125, 'y': 2331.0}, 'pointUp': {'x': 4672.0001220703125, 'y': 408.0}, 'weight': 0.912015974521637, 'detect_bottom': 1, 'detect_door': 0, 'balconydoor': 0}]] }"
json_txt = json_txt.replace('\'', '"')
json.loads(json_txt)

这样就不会有任何错误了!!!

补充:

也可以使用三引号作为外层,双引号直接作为内部的引号,例子如下:

# 下面这两种三引号的写法都是可以的
json_txt = '''{"doors": [[{"pointDown": {"x": 3307.0001220703125, "y": 2367.75}, "pointUp": {"x": 3307.0001220703125, "y": 408.0}, "weight": 0.912015974521637, "detect_bottom": 1, "detect_door": 0, "balconydoor": 0}, {"pointDown": {"x": 4672.0001220703125, "y": 2331.0}, "pointUp": {"x": 4672.0001220703125, "y": 408.0}, "weight": 0.912015974521637, "detect_bottom": 1, "detect_door": 0, "balconydoor": 0}]] }'''
# json_txt = """{"doors": [[{"pointDown": {"x": 3307.0001220703125, "y": 2367.75}, "pointUp": {"x": 3307.0001220703125, "y": 408.0}, "weight": 0.912015974521637, "detect_bottom": 1, "detect_door": 0, "balconydoor": 0}, {"pointDown": {"x": 4672.0001220703125, "y": 2331.0}, "pointUp": {"x": 4672.0001220703125, "y": 408.0}, "weight": 0.912015974521637, "detect_bottom": 1, "detect_door": 0, "balconydoor": 0}]] }"""
json.loads(json_txt)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值