April 02, 2011

New Line Problem in JSON

- I have problem when retrieving JSON formatted text from multiline textbox.
- I have HANG UP problem when call a method with a JSON text with new line.
- I've generated some JSON from a textbox and i have error when the user press enter between text

OK, the solution is so simple, you must just replace the newline characters with a another valid character, yes, it's works.
str = str.replace("\n", "\\n");
str = str.replace("\n", "");
but it's works just for change first newline and for change all new line must be follow of /TEXT/g regular expression and enjoy ;-)
str = str.replace(/\n/g, "\\n");
str = str.replace(/\n/g, "");

[1] RegExp Object


Anonymous said...

Simple trick,
But for me great help.

David Spector said...

When using any form of Ajax, detailed documentation for the format of responses received from the CGI server seems to be lacking on the Web. Some Notes here and entries at stackoverflow.com point out that newlines in returned text or json data must be escaped to prevent infinite loops (hangs) in JSON conversion (possibly created by throwing an uncaught exception), whether done automatically by jQuery or manually using Javascript system or library JSON parsing calls.

In each case where programmers post this problem, inadequate solutions are presented (most often replacing \n by \\n (backslash-n by backslash-backslash-n) on the sending side) and the matter is dropped. Their inadequacy is revealed when passing string values that accidentally embed control escape sequences, such as Windows pathnames. An example is "C:\Chris\Roberts.php", which contains the control characters ^c and ^r, which can cause JSON conversion of the string {"file":"C:\Chris\Roberts.php"} to loop forever. One way of generating such values is deliberately to attempt to pass PHP warning and error messages from server to client, a reasonable idea.

By definition, Ajax uses HTTP connections behind the scenes. Such connections pass data using GET and POST, both of which require encoding sent data to avoid incorrect syntax, including control characters.

This gives enough of a hint to construct what seems to be a solution (it needs more testing): to use rawurlencode on the PHP (sending) side to encode the data, and unescape on the Javascript (receiving) side to decode the data. In some cases, you will apply these to entire text strings, in other cases you will apply them only to values inside JSON.

If this idea turns out to be correct, simple examples can be constructed to help programmers at all levels solve this problem once and for all.