开发者

Is it possible to encapsulate multiple-lines of html or javascript inside Json format?

开发者 https://www.devze.com 2022-12-31 00:16 出处:网络
Any 开发者_如何学运维example on the net ?Yes. Newlines must be e escaped as \\n in a JSON string.Another option would be to create an JSON array, where each element is a new line of the string. The re

Any 开发者_如何学运维example on the net ?


Yes. Newlines must be e escaped as \n in a JSON string.


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"]
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消