Any example on the net ?
views:
57answers:
3
+2
A:
Another option would be to create an JSON array, where each element is a new line of the string. The reading program could then join it with new lines.
So instead of:
{
value : "string with\nline breaks"
}
you could do
{
value : ["string with",
"line breaks"]
}
Eric
2010-05-16 13:29:21
Why? The `\n` escape sequence is a defined part of JSON, it works right-away. The array requires special handling on the client *and* on the server, plus remembering that in special places in your JSON an array is not meant to be an array, plus a way of differentiating a *real* array of strings from a string that's supposed to be joined. Much too much hassle to emulate an effect that has been built into JSON from the start.
Tomalak
2010-05-16 15:07:05
If the JSON is human created, then my solution is more intuitive. If however, it's solely for transferring data between two automatic users, then it's better to use `\n`.
Eric
2010-05-16 15:25:22
If human created, there is search-and-replace in text editors. ;)
Tomalak
2010-05-16 18:28:29
+1
A:
You've tagged this with PHP, so assuming you're using PHP, you should be able to prove that it works using something like:
$html_string = "<b>Hello, world!</b>\n<i>It's a beautiful day...</i>";
$json = json_encode(array( 'html' => $html_string ));
Now, you also mentioned including Javascript in your JSON response. That might be a bit more troublesome. Depending on how you place the HTML content in your page (blindly assuming here that you're using ajax), you may need to take special steps to make the Javascript execute. Please consult your ajax library's documentation for more information on that.
Charles
2010-05-16 15:49:09