Can a JSON value contain a multiline string

前端 未结 5 1270
粉色の甜心
粉色の甜心 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:11

    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(" ")
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-13 17:20

    Not pretty good solution, but you can try the hjson tool. 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.

    P.S. Should be a comment to the question, but don't have enough repo, sorry.

    0 讨论(0)
  • 2020-12-13 17:20

    As I could understand the question is not about how pass a string with control symbols using json but how to store and restore json in file where you can split a string with editor control symbols.

    If you want to store multiline string in a file then your file will not store the valid json object. But if you use your json files 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.

    Or, alternatively, which would be better, you can have your json data source files where you edit a sting as you want and then remove all new lines with some utility to the valid json file which your program will use.

    0 讨论(0)
  • 2020-12-13 17:29

    I believe it depends on what json interpreter you're using... in plain javascript you could use line terminators

    {
      "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."
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题