如何发送json字符串作为json对象进行查看?

时间:2010-06-17 07:20:34

标签: asp.net-mvc json

我在MVC控制器的动作中有一个json字符串。我想将其作为JSON对象发送到视图。我该如何解决这个问题?

public JsonResult Json()
{
    ... some code here ...
    string jsonString = "{\"Success\":true, \"Msg\":null}";
    // what should I do instead of assigning jsonString to Data. 
    return new JsonResult() { Data = jsonString, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}

1 个答案:

答案 0 :(得分:5)

public ActionResult Json()
{
    ... some code here ...
    string jsonString = "{\"Success\":true, \"Msg\":null}";
    return Content(jsonString, "application/json");
}

但我建议您使用对象而不是字符串:

public ActionResult Json()
{
    ... some code here ...
    return Json(
        new 
        {
            Success = true,
            Msg = (string)null
        }, 
        JsonRequestBehavior.AllowGet
    );
}