有下面一段字符串
import json
str0 = '[{"name":"白云大道营业厅","siteaddr":"x...","businesshour":"x","introduction":"","sitetype":"","auto":"","sitepic":"x","detail":"x","environment":"",\
"busline":"","isOpen":0,"icon":{w:23,h:25,l:21,t:21,x:6,lb:5]'
ret = json.loads(str0)
但是运行之后会报
JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 175 (char 174)
发现就是icon后面那一串字符串不规则,没有引号,比较麻烦的方法就是程序加上引号,再序列化就可以了,但是python有那么多方法,那么多包。
下面说2种方法
- 法1
eval(str0, type('Dummy', (dict,), dict(__getitem__=lambda s,n:n))())
- 法2
import demjson
ret = demjson.decode(str0)