How do I handle newlines in JSON?

后端 未结 10 1541
后悔当初
后悔当初 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 05:53

    I guess this is what you want:

    var data = '{"count" : 1, "stack" : "sometext\\n\\n"}';
    

    (You need to escape the "\" in your string (turning it into a double-"\"), otherwise it will become a newline in the JSON source, not the JSON data.)

提交回复
热议问题