开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜