April 02, 2011

New Line Problem in JSON

Problem
- 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

Solution
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");
or
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");
or
str = str.replace(/\n/g, "");

References
[1] RegExp Object

1 comment:

Anonymous said...

Simple trick,
But for me great help.