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

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

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


当前回答

假设这个问题与轻松编辑文本文件,然后手动将它们转换为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不允许真正的换行。您需要将所有换行符替换为\n。

eg:

"first line
second line"

可以通过以下方式保存:

“一线/二线”

注意:

对于Python,这应该写成:

“一线、二线”

\\是用来转义反斜杠的,否则python会把\n当作 控制字符“new 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,但它有其他缺陷,并且本机不支持。

假设这个问题与轻松编辑文本文件,然后手动将它们转换为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可以将其解释为换行转义。

不幸的是,这里的许多答案都解决了如何在字符串数据中放入换行符的问题。问题是如何通过将字符串值拆分到多行代码中来使代码看起来更好。(即使是认识到这一点的答案也提供了假设人们可以自由改变数据表示的“解决方案”,而在许多情况下,人们并不是这样。)

更糟糕的是,没有好的答案。

在许多编程语言中,即使它们不显式地支持跨行分割字符串,您仍然可以使用字符串连接来获得所需的效果;只要编译器不是那么糟糕就可以了。

但是json不是一种编程语言;它只是一个数据表示。你不能告诉它去连接字符串。它的语法(相当小)也不包括任何在多行上表示字符串的工具。

除了设计某种类型的预处理器(就我个人而言,我不想有效地创造自己的语言来解决这个问题),这个问题没有一个通用的解决方案。如果可以更改数据格式,则可以替换字符串数组。否则,这就是json不是为人类可读性而设计的众多方面之一。