python json.loads兼容单引号数据的方法
Python的json模块解析单引号数据会报错,示例如下
>>> import json >>> data = "{'field1': 0, 'field2': 'hehehehe', 'field3': 'hahaha'}" >>> json.loads(data)
Traceback (most recent call last): File “”, line 1, in File “/usr/lib/python3.5/json/init.py”, line 319, in loads return _default_decoder.decode(s) File “/usr/lib/python3.5/json/decoder.py”, line 339, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File “/usr/lib/python3.5/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)
摸索的解决办法如下
>>> data = json.dumps(eval(data)) >>> print(data)
{“field3”: “hahaha”, “field2”: “hehehehe”, “field1”: 0}
处理后正确解析
>>> print(json.loads(data))
{‘field3': ‘hahaha', ‘field2': ‘hehehehe', ‘field1': 0}
相关推荐
chognzhihongseu 2020-01-24
qshpeng 2010-12-29
麋鹿麋鹿迷了路 2011-07-20
littlecushion000 2014-08-04
puddingpp 2017-12-18
whitecat 2019-05-09
狂浪的旮旯天地 2013-09-10
成功IT人 2012-11-13
红薯藤 2015-11-14
ALiDan 2019-04-18
hellowillness 2011-09-27
DaemonFG 2019-04-10
dropkai 2009-11-26
ApacheMySQL 2019-04-06
huiqinbo 2009-06-27
shilukun 2019-04-04
caodayong 2019-04-03
ljinshuo 2007-05-12