转义单引号JSON

编程入门 行业动态 更新时间:2024-10-25 07:34:59
本文介绍了转义单引号JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用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

本文发布于:2023-10-27 23:38:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1534866.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单引号   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!