Can a JSON value contain a multiline string

前端 未结 5 1282
粉色の甜心
粉色の甜心 2020-12-13 16:43

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

{
  \"testCases\" :
  {
    \"case.1\" :
    {
      \"scenario         


        
5条回答
  •  轮回少年
    2020-12-13 17:16

    Per the specification, the JSON grammar's char production can take the following values:

    • any-Unicode-character-except-"-or-\-or-control-character
    • \"
    • \\
    • \/
    • \b
    • \f
    • \n
    • \r
    • \t
    • \u four-hex-digits

    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.

    The JSONLint tool confirms that your JSON is invalid.


    And, if you want to write newlines inside your JSON syntax without actually including newlines in the data, then you're 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.

提交回复
热议问题