goofansu
goofansu

Reputation: 2277

python json.loads on str

maybe the question is silly.

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

json.loads(s) bring an exception:

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)

while json.loads(t) is right:

{u'x': 1}

what's the different? Thank you in advance.

Upvotes: 0

Views: 519

Answers (2)

Gagandeep Singh
Gagandeep Singh

Reputation: 5879

If you look at Json specification, it allows double quotes only as proper string.

Upvotes: 1

Karl Knechtel
Karl Knechtel

Reputation: 61515

JSON format requires double-quoted strings in the data.

Upvotes: 4

Related Questions