views:

149

answers:

3

returning HTML in my json array, what things do I have to escape for?

A: 

HTML can be passed in JSON if standard JSON escaping rules are applied. Any json library (worth its weight in bytes) will do this for you.

In PHP:

json_encode('<body class="foo">');

Returns

"<body class=\"foo\">"

More info on http://www.json.org/

gahooa
A: 

I think the answer is, you don't need to. JSON encoding will handling everything for you.

However, depending on your other needs, if you want to strip tags, or make < into &lt;, then you can do so to the HTML string first, or do it at the client using Javascript.

動靜能量
A: 

You don't need to escape anything. The Json serializer will take care of it:

return Json(new { html = "<html><body><div class=\"foo\">Hello</div></body></html>" });
Darin Dimitrov