I am converting an object to JSON using JavaScriptSerializer
and I can see this JSON output in server code:
[{"UserId":1,"UserName":"Admin"}]
But in the UI it's getting converted to something like below
[{"UserId":1,"UserName":"Admin"}].
How to escape those "
?
Why are you doing that? Why not just return a
JsonResult
?If you are using the Razor view engine you need to use the
Html.Raw
method:Notice the usage of the
Json.Encode
method which is shorter and equivalent tonew JavaScriptSerializer().Serialize()
.Just one more thing on Darin Dimitrov's answer. In my VS2012 there is a compilation error with the semicolon, cuz the statement from JS side is actually "var model = ;". A way around using a pair of quotation to wrap the Razor part like this:
This will not cause any error.
Json.Encode() seems to be a wrapper function of JavaScriptSerializer. I'm not sure if the latter is more time efficient.