是否有可能在JSON中有多行字符串?
这主要是为了视觉上的舒适,所以我想我可以在编辑器中打开自动换行,但我只是有点好奇。
我正在编写JSON格式的一些数据文件,并希望有一些非常长的字符串值分割在多行。使用python的JSON模块,无论我使用\或\n作为转义,我都会得到很多错误。
是否有可能在JSON中有多行字符串?
这主要是为了视觉上的舒适,所以我想我可以在编辑器中打开自动换行,但我只是有点好奇。
我正在编写JSON格式的一些数据文件,并希望有一些非常长的字符串值分割在多行。使用python的JSON模块,无论我使用\或\n作为转义,我都会得到很多错误。
当前回答
这是一个非常老的问题,但当我想提高使用复杂条件表达式的Vega JSON规范代码的可读性时,我也有同样的问题。代码是这样的。
正如这个答案所说,JSON不是为人类设计的。我知道这是一个历史性的决定,它对数据交换的目的是有意义的。然而,JSON仍然被用作这种情况下的源代码。所以我要求我们的工程师使用Hjson作为源代码,并将其处理成JSON。
例如,在Git For Windows环境中, 你可以下载Hjson命令行的二进制文件,放在git/bin目录下使用。 然后,转换(转译)Hjson源为JSON。使用自动化工具(如Make)将有助于生成JSON。
$ which hjson
/c/Program Files/git/bin/hjson
$ cat example.hjson
{
md:
'''
First line.
Second line.
This line is indented by two spaces.
'''
}
$ hjson -j example.hjson > example.json
$ cat example.json
{
"md": "First line.\nSecond line.\n This line is indented by two spaces."
}
如果要在编程语言中使用转换后的JSON,特定于语言的库(如hjson-js)将非常有用。
我注意到在一个重复的问题中张贴了同样的想法,但我想分享更多的信息。
其他回答
这是一个非常老的问题,但当我想提高使用复杂条件表达式的Vega JSON规范代码的可读性时,我也有同样的问题。代码是这样的。
正如这个答案所说,JSON不是为人类设计的。我知道这是一个历史性的决定,它对数据交换的目的是有意义的。然而,JSON仍然被用作这种情况下的源代码。所以我要求我们的工程师使用Hjson作为源代码,并将其处理成JSON。
例如,在Git For Windows环境中, 你可以下载Hjson命令行的二进制文件,放在git/bin目录下使用。 然后,转换(转译)Hjson源为JSON。使用自动化工具(如Make)将有助于生成JSON。
$ which hjson
/c/Program Files/git/bin/hjson
$ cat example.hjson
{
md:
'''
First line.
Second line.
This line is indented by two spaces.
'''
}
$ hjson -j example.hjson > example.json
$ cat example.json
{
"md": "First line.\nSecond line.\n This line is indented by two spaces."
}
如果要在编程语言中使用转换后的JSON,特定于语言的库(如hjson-js)将非常有用。
我注意到在一个重复的问题中张贴了同样的想法,但我想分享更多的信息。
查看规格!JSON语法的char生成可以取以下值:
any-Unicode-character-except——“或者——\ -or-control-character \” \ \ \/ \ b \ f \ n r \ \ t \ u four-hex-digits
换行符是“控制字符”,所以,不,你可以在你的字符串中没有文字换行符。然而,你可以使用你需要的\n和\r的任意组合来编码它。
我看到许多的答案可能不工作在大多数情况下,但可能是最简单的解决方案如果假设你想输出你写在一个JSON文件(例如:语言翻译,你想和超过1线只有一个关键输出在客户机上)可以添加一些特殊字符您所选择的PS:允许通过JSON文件\ \新行之前和使用一些JS解析文本…如:
例子:
文件(text.json)
{"text": "一些JSON文本。\\下一行JSON文本"}
import text from 'text.json'
{text.split('\\')
.map(line => {
return (
<div>
{line}
<br />
</div>
);
})}}
将属性值写入字符串数组。比如这里给出的例子https://gun.io/blog/multi-line-strings-in-json/。这将有所帮助。
我们总是可以使用数组的字符串多行字符串,如下所示。
{
"singleLine": "Some singleline String",
"multiline": ["Line one", "line Two", "Line Three"]
}
我们可以很容易地迭代数组以多行方式显示内容。
假设这个问题与轻松编辑文本文件,然后手动将它们转换为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.