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, "stack" : "sometext\n\n"}';
var dataObj = eval('('+data+')');
This gives me an error:
unterminated string literal
With JSON.parse(data)
, I see similar error messages: "Unexpected token ↵
" in Chrome, and "unterminated string literal
" in Firefox and IE.
When I take out the \n
after sometext
the error goes away in both cases. I can't seem to figure out why the \n
makes eval
and JSON.parse
fail.
You will need to have a function which replaces
\n
to\\n
in casedata
is not a string literal.Resulting
dataObj
will be