str上的python json.loads

时间:2012-05-18 04:49:56

标签: python json

也许问题很愚蠢。

s="{'x':1}"
t='{"x":1}'

json.loads(s)带来例外:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/2.7.3/lib/python2.7/json/__init__.py", line 326, in loads
  return _default_decoder.decode(s)
File "/usr/local/Cellar/python/2.7.3/lib/python2.7/json/decoder.py", line 366, in decode
  obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/Cellar/python/2.7.3/lib/python2.7/json/decoder.py", line 382, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 1 (char 1)

而json.loads(t)是对的:

{u'x': 1}

有什么不同?提前谢谢。

2 个答案:

答案 0 :(得分:4)

JSON格式需要数据中的双引号字符串。

答案 1 :(得分:1)

如果查看Json specification,它只允许双引号作为正确的字符串。