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

>>> s="{'name':'Tom'}"
>>> type(s)
<class 'str'>
>>> json.loads(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files\python\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\Program Files\python\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Program Files\python\lib\json\decoder.py", line 353, 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)

如上,在使用json.loads将str转为dict格式的数据时报错

但如果将s="{'name':'Tom'}"改为s='{"name":"Tom"}',就可以转化成功了

>>> s='{"name":"Tom"}'
>>> type(s)
<class 'str'>
>>> S=json.loads(s)
>>> S
{'name': 'Tom'}
>>> type(S)
<class 'dict'>

看来原因是出现在引号上。。。

posted @ 2020-03-16 00:15  腹肌猿  阅读(2029)  评论(0编辑  收藏  举报