When parsing json using jQuery.parseJSON(), I get this error
SyntaxError: JSON.parse: bad control character in string literal
A quick search revelead that this is caused by newlines in the json string.
I get my json string from PHP using json_encode().
Is there a way to encode it in such a way that jQuery.parseJSON() would not complain and still retain the newline information?
According to the v8 bug tracker (http://code.google.com/p/v8/issues/detail?id=616), this is actually the correct behavior:
“Newlines are not allowed inside JSON strings (no control characters are, see, e.g.,
JsonStringCharacter production of ECMA262 5ed, section 15.2.1.1).”
So it depends really on what you want to do – in this instance, as you need the newlines, you need to escape them before doing the json parse: