简体   繁体   中英

Escaping single quotes JSON

I am using the json module to convert a series of dictionaries to json but I am not sure why when dumping to json the strings that contain a single quote (eg: My father's car) are escaped to (My father\\'s car). When I check in an online validator it saying that the format is wrong. Why dumps escape them when it is not correct?

I tried to replace the strings using replace but it does not act of them. Would that be a valid solution? Why is not working the following snipped

formatted_json = json.dumps(OrderedDict([("nodes", json_graph['nodes']), ("links", json_graph['links'])])).replace('\'',"'")

Thanks!

You probably see the value representation from python interpreter. If you print the value or store it in a file, you'll see the correct behaviour.

>>> import json
>>> json.dumps("a'b")
'"a\'b"'
>>> print json.dumps("a'b")
"a'b"

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