python json json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes

>>> import json
>>> test="{'data':'123'}"
>>> result=json.loads(test)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "d:\Anaconda3\lib\json\__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "d:\Anaconda3\lib\json\decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "d:\Anaconda3\lib\json\decoder.py", line 355, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes:
 line 1 column 2 (char 1)

原因在于:字符串里用单引号来标识字符。

解决办法如下:将字符串里的单引号替换成双引号

>>> import re
>>> test=re.sub('\'','\"',test)
>>> result=json.loads(test)
>>> result['data']
'123'

对于带u'的字符串,u也要去掉:

>>> c=f.read()
>>> c
"{u'meta': {u'code': 200, u'requestId': u'5815f6739fb6b77494061e19'}, u'response
': {u'tips': {u'count': 0, u'items': []}}}"
>>> d=re.sub("u'","\"",c)
>>> d
'{"meta\': {"code\': 200, "requestId\': "5815f6739fb6b77494061e19\'}, "response\
': {"tips\': {"count\': 0, "items\': []}}}'
>>> d=re.sub("'","\"",d)
>>> d
'{"meta": {"code": 200, "requestId": "5815f6739fb6b77494061e19"}, "response": {"
tips": {"count": 0, "items": []}}}'
>>> json.loads(d)
{'response': {'tips': {'items': [], 'count': 0}}, 'meta': {'requestId': '5815f67
39fb6b77494061e19', 'code': 200}}
>>>

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用和提到了相同的错误信息,即"json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)"。这个错误通常是由于JSON数据格式不正确导致的。在JSON中,属性名必须用双引号括起来,而不是单引号或没有引号。所以在您的情况下,错误出现在第1行第2列(即第3个字符),因为属性名没有用双引号括起来。要解决这个问题,您需要确保在JSON数据中所有的属性名都被正确地用双引号括起来。 引用提到了相同的错误信息,并提到了解决方案。解决这个问题的方法是,检查并修复JSON数据中的属性名是否正确地用双引号括起来。确保没有使用单引号或没有引号来定义属性名。您可以使用JSON验证工具或在线JSON验证器来验证您的JSON数据的格式,并找出并修复错误的属性名。 总结起来,"json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 3 (char 2)"错误是由于JSON数据中有属性名没有正确地用双引号括起来导致的。要解决这个问题,需要检查并修复JSON数据中的属性名是否正确地用双引号括起来。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [【pythonjson.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 ...](https://blog.csdn.net/august_shi/article/details/131855068)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 colum原因及...](https://blog.csdn.net/weixin_44493291/article/details/125998079)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值