在asp.net mvc中返回名为json的对象

时间:2013-09-20 16:22:52

标签: c# json asp.net-mvc-4

我试图让我的控制器返回一个命名的JSON数组。它目前返回格式如下:

[{"Title":"Star Wars: A New Hope","Year":1977},{"Title":"Star Wars: The Empire Strikes Back","Year":1980},{"Title":"Star Wars: Return of the Jedi","Year":1983}]

但我希望它像这样

movies:[{"Title":"Star Wars: A New Hope","Year":1977},{"Title":"Star Wars: The Empire Strikes Back","Year":1980},{"Title":"Star Wars: Return of the Jedi","Year":1983}]

这是我的控制器方法:

public JsonResult GetMovies()
{
    var model = _movies;


    return Json(model, JsonRequestBehavior.AllowGet);
}

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

您可以返回匿名json对象,如

 return Json(new {
        movies = model
    } , JsonRequestBehavior.AllowGet);