简体   繁体   中英

Simplejson strange behavior

So In python I make a dictionary in JSON structure

>>> a = {"name":'nikhil',"age":25}

Now I check if a is a Valid JSON using http://jsonlint.com/ . I get it's valid.

Now I do :

>>> b = simplejson.dumps(a)
>>> b= '{"age": 25, "name": "nikhil"}'

Now I do:

>>> c = simplejson.loads(b)
>>> c = {'age': 25, 'name': 'nikhil'}

Now I check if c is a Valid JSON I get error.

Why is Simplejson is not able to convert JSON string back to valid JSON? when I started with a valid JSON only?

You are confusing JSON with Python here. b is a JSON-formatted string, c is a Python object .

Python syntax just happens to look a lot like JSON (JavaScript) in that respect.

  • Python strings can use either ' or " , depending on the contents; JSON always uses " . You entered a using double quotes for the keys, single quotes for the one string value; if you asked Python to echo it back for you you'll find it'll be shown with only single quotes.

  • Python booleans are True or False , JSON uses true and false .

  • The JSON 'empty' value is null , Python uses None instead.

See the Encoders and Decoders section of the json module for an overview how JSON and Python objects are mapped.

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