您好,欢迎访问一九零五行业门户网

python的json解析函数不支持单引号字符串

使用python标准库的json解析函数json.loads是不能使用单引号引用的json字符串,示例如下:
改用双引号,或者在loads之前先调用json.dumps(a)也可以。
>>> a = "{'aa':'dd','dd':'df'}" >>> import json >>> b = json.loads(a) traceback (most recent call last): file "<stdin>", line 1, in <module> file "/usr/lib64/python2.6/json/__init__.py", line 307, in loads return _default_decoder.decode(s) file "/usr/lib64/python2.6/json/decoder.py", line 319, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) file "/usr/lib64/python2.6/json/decoder.py", line 336, in raw_decode obj, end = self._scanner.iterscan(s, **kw).next() file "/usr/lib64/python2.6/json/scanner.py", line 55, in iterscan rval, next_pos = action(m, context) file "/usr/lib64/python2.6/json/decoder.py", line 171, in jsonobject raise valueerror(errmsg("expecting property name", s, end)) valueerror: expecting property name: line 1 column 1 (char 1) >>> a = '{"aa":"asdf","dd":"dfads"} file "<stdin>", line 1 a = '{"aa":"asdf","dd":"dfads"} ^ syntaxerror: eol while scanning string literal >>> a = '{"aa":"asdf","dd":"dfads"}' >>> b = json.loads(a) >>> b >>> <span style="font-family: helvetica, tahoma, arial, sans-serif; line-height: 1.5; background-color: white;">{u'aa': u'asdf', u'dd': u'dfads'}</span><span style="font-family: helvetica, tahoma, arial, sans-serif; line-height: 1.5; background-color: white;"> </span>
其它类似信息

推荐信息