使用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>