我正在使用json模块将一系列字典转换为json,但是我不确定为什么将包含单引号的字符串(例如:我父亲的车)转储到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 (e.g: 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?
我尝试使用replace替换字符串,但是它不起作用.那将是一个有效的解决方案吗?为什么以下内容无法正常工作
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('\'',"'")谢谢!
推荐答案您可能会看到python解释器的值表示形式.如果您打印该值或将其存储在文件中,您将看到正确的行为.
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"更多推荐
转义单引号JSON
发布评论