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

查看:81
本文介绍了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格式编写一些数据文件,并希望将一些很长的字符串值分成多行.无论使用\还是\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 不允许使用实际的换行符.您需要将所有换行符替换为\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屋!

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