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

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

我正在编写JSON格式的一些数据文件,并希望有一些非常长的字符串值分割在多行。使用python的JSON模块,无论我使用\或\n作为转义,我都会得到很多错误。


当前回答

如果只是为了在编辑器中显示,你可以使用'代替'或'

const obj = {
myMultiLineString: `This is written in a \
multiline way. \
The backside of it is that you \
can't use indentation on every new \
line because is would be included in \
your string. \
The backslash after each line escapes the carriage return. 
`
}

例子:

console.log(`First line \
Second line`);

将放在控制台: 一线二线

console.log(`First line 
second line`);

将放在控制台: 第一行 第二行

希望这回答了你的问题。

其他回答

查看规格!JSON语法的char生成可以取以下值:

any-Unicode-character-except——“或者——\ -or-control-character \” \ \ \/ \ b \ f \ n r \ \ t \ u four-hex-digits

换行符是“控制字符”,所以,不,你可以在你的字符串中没有文字换行符。然而,你可以使用你需要的\n和\r的任意组合来编码它。

假设这个问题与轻松编辑文本文件,然后手动将它们转换为json有关,我发现了两个解决方案:

hjson (that was mentioned in this previous answer), in which case you can convert your existing json file to hjson format by executing hjson source.json > target.hjson, edit in your favorite editor, and convert back to json hjson -j target.hjson > source.json. You can download the binary here or use the online conversion here. jsonnet, which does the same, but with a slightly different format (single and double quoted strings are simply allowed to span multiple lines). Conveniently, the homepage has editable input fields so you can simply insert your multiple line json/jsonnet files there and they will be converted online to standard json immediately. Note that jsonnet supports much more goodies for templating json files, so it may be useful to look into, depending on your needs.

这是一个非常老的问题,但我在搜索中发现了这个问题,我想我知道你问题的根源了。

JSON不允许在其数据中出现“真正的”换行符;它只能有换行符。请看来自@YOU的回答。根据问题,看起来您试图在Python中转义换行有两种方式:使用行延续字符(“\”)或使用“\n”作为转义符。

但请记住:如果你在python中使用字符串,特殊转义字符(“\t”,“\n”)将被转换为REAL控制字符!“\n”将被替换为表示换行符的ASCII控制字符,而换行符正是JSON中不合法的字符。(至于换行字符,它只是把换行符去掉。)

所以你需要做的是防止Python转义字符。你可以通过使用一个原始字符串(将r放在字符串前面,如r"abc\ndef",或者在换行符前包含一个额外的斜杠("abc\\ndef")来做到这一点。

上述两种方法都不会将“\n”替换为真正的换行ASCII控制字符,而是将“\n”保留为两个文字字符,然后JSON可以将其解释为换行转义。

如果只是为了在编辑器中显示,你可以使用'代替'或'

const obj = {
myMultiLineString: `This is written in a \
multiline way. \
The backside of it is that you \
can't use indentation on every new \
line because is would be included in \
your string. \
The backslash after each line escapes the carriage return. 
`
}

例子:

console.log(`First line \
Second line`);

将放在控制台: 一线二线

console.log(`First line 
second line`);

将放在控制台: 第一行 第二行

希望这回答了你的问题。

我曾经在一个小型Node.js项目中这样做过,并发现这个变通方法可以将多行字符串存储为行数组,使其更易于人类阅读(代价是稍后将它们转换为字符串的额外代码):

{
 "modify_head": [

  "<script type='text/javascript'>",
  "<!--",
  "  function drawSomeText(id) {",
  "  var pjs = Processing.getInstanceById(id);",
  "  var text = document.getElementById('inputtext').value;",
  "  pjs.drawText(text);}",
  "-->",
  "</script>"

 ],

 "modify_body": [

  "<input type='text' id='inputtext'></input>",
  "<button onclick=drawSomeText('ExampleCanvas')></button>"
 
 ],
}

一旦解析,我只使用myData.modify_head.join('\n')或myData.modify_head.join(),这取决于我是否想在每个字符串后换行。

这看起来很整洁,除了我必须在所有地方使用双引号。尽管在其他情况下,我也许可以使用YAML,但它有其他缺陷,并且本机不支持。