JSON值可以包含多行字符串吗 [英] Can a JSON value contain a multiline string

查看:357
本文介绍了JSON值可以包含多行字符串吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个JSON文件,该文件将由Java程序读取.片段如下...

I am writing a JSON file which would be read by a Java program. The fragment is as follows...

{
  "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 生产可以采用以下值:

Check out the specification! The JSON grammar's char production can take the following values:

  • 任何Unicode字符,除了" -or- \ -or-control-character
  • \"
  • \\
  • \/
  • \b
  • \f
  • \n
  • \r
  • \t
  • \u 四个十六进制数字
  • any-Unicode-character-except-"-or-\-or-control-character
  • \"
  • \\
  • \/
  • \b
  • \f
  • \n
  • \r
  • \t
  • \u four-hex-digits

换行符是控制字符",因此,不,您的字符串中可能没有文字换行符.但是,您可以使用所需的\n\r的任意组合对其进行编码.

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 \n and \r you require.

JSONLint 工具确认您的JSON无效.

The JSONLint tool confirms that your JSON is invalid.

更新:而且,如果您想在JSON语法中编写换行符而又没有在数据中实际包含换行符,那么您甚至会感到很不幸.尽管JSON在某种程度上是对人类友好的,但它仍然是 data ,您正在尝试将任意格式应用于该数据.绝对不是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 data and you're trying to apply arbitrary formatting to that data. That is absolutely not what JSON is about.

这篇关于JSON值可以包含多行字符串吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆