Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JsonResult with Html encoding

Tags:

c#

asp.net-mvc

I have to return a JsonResult, which contains some HTML.

so, something like:

return Json(new { id="guid", html="<param id='id'/>" });

However, when I get the result back , the angle brackets are encoded as u003e, u003c , etc..

I tried various encoders but can't figure this one out. Anyone run into this?

I can return a Content(string) with the Response.ContentType = "application/json" without this issue, but my json object is pretty complex and I would prefer to return a JsonResult if possible.

Thanks!

like image 424
Paul Avatar asked Jan 26 '10 21:01

Paul


People also ask

Can you have HTML in JSON?

This article is going to assume that you're trying to insert HTML directly inside the JSON file. The problem with HTML inside JSON is that HTML elements have properties that must use double quotes. But as soon as you insert double quotes it's going to cause syntax error because JSON can only do double quotes.

Should JSON be HTML encoded?

So if you are intending to write JSON into the html as part of Javascript (a very common use), you need to encode JSON contents to HTML unfortunately. It's much better to avoid this and download data separately. those are some very good points!

How escape HTML tag JSON?

However, if your data contains HTML, there are certain things that you need to do to keep the browser happy when using your JSON data within Javascript. Escape the forward slash in HTML end tags. <div>Hello World! <\/div> .


1 Answers

That's just Unicode. Once you show it in the browser it will look fine (if the browser is your client application).

like image 104
Luca Matteis Avatar answered Nov 15 '22 10:11

Luca Matteis