I'm getting the following string:
var str='{"message":"hello\nworld"}';
I need to turn it into JSON object. However, I get an exception when I try JSON.parse(str)
because of the \n
I saw this question but it did not help.
From that, I tried
var j=JSON.parse(JSON.stringify(str))
But I'm still getting string instead of object when i use typeof j
I know using \\n
works, but the thing is, it does not print on new line when i need to use the value.
UPDATE: OK, i just realized \\n
is working.
I'm using this to convert \n
to \\n
:
var str='{"message":"hello\nworld"}';
str=str.replace(/\n/g, "\\\\n").replace(/\r/g, "\\\\r").replace(/\t/g, "\\\\t");
var json=JSON.parse(str);
console.log(json.message);
Can someone please correct it?
` tag? – Pawan Nogariya Jun 14 '16 at 11:41