Suppose I have an object variable:
var obj = { key: '\"Hello World\"' } Then I tried parse it to string by using JSON.stringify in Chrome devtools console:
JSON.stringify(obj) // "{"key":"\"Hello World\""}" I get the result "{"key":"\"Hello World\""}". Then I give it to a string
var str = '{"key":"\"Hello World\""}' At least I try to convert it back to obj:
JSON.parse(str); but the browser tell me wrong Uncaught SyntaxError
What confused me is why this is wrong? I get the string from an origin object and I just want turn it back.
How can I fix this problem? If I want do the job like convert obj to string and return it back, how can I do?
\to escape characters in this case. If you alternate between'and"you don't need to escape. For example, you can dokey: '"Hello World"'.JSON.parse(JSON.stringify(obj))will work just fine.