简体   繁体   中英

Python simplejson not converting true

Why doesn't this work? I'm reading for simplejson JsonDecoder, true should be parsable and translated to True.

% python
>>> import simplejson as json
>>> print json.loads({"bool":true})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'true' is not defined
>>>

The input to loads should be a string:

>>> json.loads('{"bool":true}')
{u'bool': True}

json.loads takes a string, which must be wrapped in quotes, like this:

o = json.loads(u'{"bool":true}')
print(o) # outputs  {u'bool': True}

Note that the u (which makes the string a character string in Python 2.x) is optional for this input and only becomes necessary if you're using non-ASCII characters such as ü, é, 编, or ℝ.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM