JSON值是否可以包含多行string

我在写一个JSON文件,这个文件可以被Java程序读取。 片段如下…

{ "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" } } } 

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

  • any-Unicode-character-except- " – 或 – \ – 或 – 控制字符
  • \"
  • \\
  • \/
  • \b
  • \f
  • \n
  • \r
  • \t
  • \u 四hex数字

换行符是“控制字符”,所以,不,你的string中可能没有字面换行符。 但是,您可以使用\n\r所需的任意组合对其进行编码。

JSONLint工具确认您的JSON无效。


更新:如果你想在你的JSON语法中写入换行符而不在数据中包含换行符,那么你甚至双倍的运气。 虽然JSON的目的是在某种程度上是人性化的,但它仍然是数据 ,你正试图对这些数据应用任意的格式。 这绝对不是JSON的。

我不确定你的具体要求,但提高“可读性”的一个可能的解决scheme是将其作为一个数组存储。

 { "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."] } } } } 

在需要时再次join

 result.join(" ") 

正如我所能理解的问题不是关于如何使用json传递带有控制符号的string,而是如何在文件中存储和恢复json,您可以使用编辑器控制符号来分割string。

如果你想在一个文件中存储多行string,那么你的文件将不会存储有效的json对象。 但是,如果您只在程序中使用json文件,那么您可以根据需要存储数据,并且每次将其加载到程序中时手动删除文件中的所有换行符,然后传递给jsonparsing器。

或者,更好的办法是,你可以将json数据源文件放在需要编辑的地方,然后用一些实用工具将所有新行删除到程序将要使用的有效json文件中。

这是作为编写器实现的,因为对于单个字符可能有多个输出字符。 我无法想象这是读者。 相当沉重的任务,但相当可扩展。

 String multilineJson = "{\n" + "prop1 = \"value1\",\n" + "prop2 = \"multi line\n" + "value2\"\n" + "}\n"; String multilineJsonExpected = "{\n" + "prop1 = \"value1\",\n" + "prop2 = \"multi line\\nvalue2\"\n" + "}\n"; StringWriter sw = new StringWriter(); JsonProcessor jsonProcessor = new JsonProcessor(sw); jsonProcessor.write(multilineJson); assertEquals(multilineJsonExpected, sw.toString()); 

履行

 public class JsonProcessor extends FilterWriter { private char[] curr; private int currIdx; private boolean doubleQuoted; public JsonProcessor(Writer out) { super(out); } @Override public void write(String str) throws IOException { char[] arr = str.toCharArray(); write(arr, 0, arr.length); } @Override synchronized public void write(char[] cbuf, int off, int len) throws IOException { curr = Arrays.copyOfRange(cbuf, off, len - off); for (currIdx = 0; currIdx < curr.length; currIdx++) { processChar(); } } private void processChar() throws IOException { switch (currentChar()) { case '"': processDoubleQuotesSymbol(); break; case '\n': case '\r': processLineBreakSymbol(); break; default: write(currentChar()); break; } } private void processDoubleQuotesSymbol() throws IOException { doubleQuoted = !doubleQuoted; write('"'); } private void processLineBreakSymbol() throws IOException { if (doubleQuoted) { write('\\'); write('n'); if (lookAhead() == '\n' || lookAhead() == '\r') { currIdx++; } } else { write(currentChar()); } } private char currentChar() { return curr[currIdx]; } private char lookAhead() { if (currIdx >= curr.length) { return 0; } return curr[currIdx + 1]; } } 

不是很好的解决scheme,但你可以试试hjson工具。 链接 。 它允许在编辑器中多行编写文本,然后将其转换为适当的有效JSON forman。 注意:它为新行添加了'\ n'字符,但是您可以在任何文本编辑器中用“Replcae all ..”函数简单地删除它们。 希望它会有所帮助。

PS应该是一个评论querstion,但没有足够的回购,对不起。

我相信这取决于你使用的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." } } }