JSON 中是否允许多行字符串? [英] Are multi-line strings allowed in JSON?

查看:54
本文介绍了JSON 中是否允许多行字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSON 中是否可以有多行字符串?

Is it possible to have multi-line strings in JSON?

这主要是为了视觉上的舒适,所以我想我可以在我的编辑器中打开自动换行,但我只是有点好奇.

It's mostly for visual comfort so I suppose I can just turn word wrap on in my editor, but I'm just kinda curious.

我正在以 JSON 格式编写一些数据文件,并希望将一些非常长的字符串值拆分为多行.使用 python 的 JSON 模块,我得到了很多错误,无论是使用 还是 作为转义.

I'm writing some data files in JSON format and would like to have some really long string values split over multiple lines. Using python's JSON module I get a whole lot of errors, whether I use or as an escape.

推荐答案

JSON 不允许真正的换行符.您需要用 替换所有换行符.

JSON does not allow real line-breaks. You need to replace all the line breaks with .

例如:

"第一行第二行"

可以保存为:

"第一行 第二行"

"first line second line"

注意:

对于Python,这应该写成:

第一行\n第二行"

其中 \ 用于转义反斜杠,否则 python 会将 视为控制字符换行"

where \ is for escaping the backslash, otherwise python will treat as the control character "new line"

这篇关于JSON 中是否允许多行字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆