简体   繁体   中英

Escaping Json in Mvc2

My controller has this:

public JsonResult Json()
        {
            return Json(myJsonObject);
        }

My issue is that the returned json needs to be somewhat escaped like so:

json.Replace("\"", "\\\"");

So ideally I would return something like

JavaScriptSerializer.Serialize(myJsonObject).Replace("\"", "\\\"")

but I dont know how to do that with JsonResult.

Any help is greatly appriciated

--MB

You can write a custom actionresult in ASP.Net MVC 2 so you could create a class in your project called EscapedJsonResult and ensure it uses the ActionResult as its base class. This means you can enclose your formatting into one action result that you can then use when ever you need to format the JSON in the way you require.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM