JSON.stringify escapes double quotes every time when stringified

前端 未结 3 850
遇见更好的自我
遇见更好的自我 2020-12-14 15:53

I am storing JSON objects retreived from web service to objects in javascript. In many places this gets stringified(This obj goes through some plugins and it strigifies and

相关标签:
3条回答
  • 2020-12-14 16:27

    What did you expect to happen?

    JSON.stringify does not act like an "identity" function when called on data that has already been converted to JSON. By design, it will escape quote marks, backslashes, etc.

    You need to call JSON.parse() exactly as many times as you called JSON.stringify() to get back the same object you put in.

    0 讨论(0)
  • 2020-12-14 16:28

    You can avoid that simply by calling JSON.stringify() exactly once on the data you want turn into JSON.

    0 讨论(0)
  • 2020-12-14 16:31

    Try

    JSON.stringify(s).replace(/\\"/g, '"')
    
    0 讨论(0)
提交回复
热议问题