JSON 值可以包含多行字符串吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16690101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can a JSON value contain a multiline string
提问by user2409155
I am writing a JSON file which would be read by a Java program. The fragment is as follows...
我正在编写一个可由 Java 程序读取的 JSON 文件。片段如下...
{
"testCases" :
{
"case.1" :
{
"scenario" : "this the case 1.",
"result" : "this is a very long line which is not easily readble.
so i would like to write it in multiple lines.
but, i do NOT require any new lines in the output.
I need to split the string value in this input file only.
such that I don't require to slide the horizontal scroll again and again while verifying the correctness of the statements.
the prev line, I have shown, without splitting just to give a feel of my problem"
}
}
}
回答by Lightness Races in Orbit
Check out the specification! The JSON grammar's charproduction can take the following values:
查看规格!JSON 语法的char生成可以采用以下值:
- any-Unicode-character-except-
"-or-\-or-control-character \"\\\/\b\f\n\r\t\ufour-hex-digits
- any-Unicode-character-except-
"-or-\-or-control-character \"\\\/\b\f\n\r\t\u四位十六进制数字
Newlines are "control characters" so, no, you may not have a literal newline within your string. However you may encode it using whatever combination of \nand \ryou require.
换行符是“控制字符”,因此,不,您的字符串中可能没有文字换行符。但是,您可以使用您需要的\n和 的任何组合对其进行编码\r。
The JSONLinttool confirms that your JSON is invalid.
该JSONLint工具确认您的JSON是无效的。
Update:And if you want to write newlines inside your JSON syntax without actually including newlines in the data, then you're even doubly out of luck. While JSON is intended to be human-friendly to a degree, it is still dataand you're trying to apply arbitrary formatting to that data. That is absolutely not what JSON is about.
更新:如果你想在你的 JSON 语法中编写换行符而不实际在数据中包含换行符,那么你的运气甚至会加倍。虽然 JSON 旨在在一定程度上对人类友好,但它仍然是数据,您正在尝试将任意格式应用于该数据。这绝对不是 JSON 的内容。
回答by Brian McAuliffe
I'm not sure of your exact requirement but one possible solution to improve 'readability' is to store it as an array.
我不确定您的确切要求,但提高“可读性”的一种可能解决方案是将其存储为数组。
{
"testCases" :
{
"case.1" :
{
"scenario" : "this the case 1.",
"result" : ["this is a very long line which is not easily readble.",
"so i would like to write it in multiple lines.",
"but, i do NOT require any new lines in the output."]
}
}
}
}
The join in back again whenever required with
需要时再次加入
result.join(" ")
回答by CodeMonkey
Not pretty good solution, but you can try the hjsontool. Link. It allows you to write text multi-lined in editor and then converts it to the proper valid JSON format. Note:it adds '\n' characters for the new lines, but you can simply delete them in any text editor with the "Replace all.." function.
不是很好的解决方案,但您可以尝试使用hjson工具。链接。它允许您在编辑器中编写多行文本,然后将其转换为正确的有效 JSON 格式。注意:它为新行添加了 '\n' 字符,但您可以在任何文本编辑器中使用“全部替换..”功能简单地删除它们。
P.S. Should be a comment to the question, but don't have enough repo, sorry.
PS 应该是对问题的评论,但没有足够的回购,抱歉。
回答by JustAnotherCurious
As I could understand the question is not about how pass a string with control symbols using jsonbut how to store and restore json in file where you can split a string with editor control symbols.
正如我所理解的,问题不在于如何使用控制符号传递字符串,json而是如何在文件中存储和恢复 json,您可以在其中使用编辑器控制符号拆分字符串。
If you want to store multiline string in a file then your file will not store the valid jsonobject. But if you use your jsonfiles in your program only, then you can store the data as you wanted and remove all newlines from file manually each time you load it to your program and then pass to json parser.
如果您想在文件中存储多行字符串,那么您的文件将不会存储有效json对象。但是,如果您json仅在程序中使用文件,那么您可以根据需要存储数据,并在每次将文件加载到程序然后传递给 json 解析器时手动从文件中删除所有换行符。
Or, alternatively, which would be better, you can have your jsondata source files where you edit a sting as you want and then remove all new lines with some utility to the valid jsonfile which your program will use.
或者,或者,这会更好,您可以将json数据源文件放在您想要的地方,您可以根据需要编辑一个字符串,然后使用一些实用程序将所有新行删除到json您的程序将使用的有效文件中。
回答by Elric Best
I believe it depends on what json interpreter you're using... in plain javascript you could use line terminators
我相信这取决于你使用的是什么 json 解释器......在普通的 javascript 中你可以使用行终止符
{
"testCases" :
{
"case.1" :
{
"scenario" : "this the case 1.",
"result" : "this is a very long line which is not easily readble. \
so i would like to write it in multiple lines. \
but, i do NOT require any new lines in the output."
}
}
}

