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!
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.
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!
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> .
That's just Unicode. Once you show it in the browser it will look fine (if the browser is your client application).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With