How do I handle newlines in JSON?

后端 未结 10 1521
后悔当初
后悔当初 2020-11-22 05:28

I\'ve generated some JSON and I\'m trying to pull it into an object in JavaScript. I keep getting errors. Here\'s what I have:

var data = \'{\"count\" : 1, \         


        
10条回答
  •  时光取名叫无心
    2020-11-22 06:02

    JSON.stringify

    JSON.stringify(`{ 
      a:"a"
    }`)
    

    would convert the above string to

    "{ \n      a:\"a\"\n    }"
    

    as mentioned here

    json stringify

    This function adds double quotes at the beginning and end of the input string and escapes special JSON characters. In particular, a newline is replaced by the \n character, a tab is replaced by the \t character, a backslash is replaced by two backslashes \, and a backslash is placed before each quotation mark.

提交回复
热议问题