JSON中的多行字符串 [英] Multiline strings in JSON
问题描述
我正在以JSON格式编写一些数据文件,并希望将一些很长的字符串值分成多行.无论使用\
还是\n
进行转义,使用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 \n
as an escape.
是否可以在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 不允许使用实际的换行符.您需要将所有换行符替换为\n
.
JSON does not allow real line-breaks. You need to replace all the line breaks with \n
.
例如:
"first line
second line"
"first line
second line"
可以保存:
"first line\nsecond line"
注意:
对于Python
,应写为:
"first line\\nsecond line"
其中\\
用于转义反斜杠,否则python将\n
视为
控制字符换行"
where \\
is for escaping the backslash, otherwise python will treat \n
as
the control character "new line"
这篇关于JSON中的多行字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!