如何将List 转换为JsonResult?

时间:2021-06-17 07:20:46

I'm trying to take a List of objects and return them as a JsonResult to an AJAX call. I'm trying this:

我正在尝试获取一个对象列表并将它们作为JsonResult返回给AJAX调用。我正在尝试这个:

List<object> list = getList();
JavaScriptSerializer jss = new JavaScriptSerializer();
JsonResult json = jss.Serialize(list);

jss.Serialize takes an object as its parameter, so this is obviously not working. Is there a way I can just pass in a List of objects and get what I need returned?

jss.Serialize将一个对象作为其参数,因此这显然不起作用。有没有办法可以传入一个对象列表并获得我需要的东西?

2 个解决方案

#1


0  

The following example shows how to return an instance of the JsonResult class from an action method. The object that is returned specifies that a GET request is permitted.

以下示例显示如何从操作方法返回JsonResult类的实例。返回的对象指定允许GET请求。

 public ActionResult Movies()
 {
     var movies = new List<object>();

     movies.Add(new { Title = "Ghostbusters", Genre = "Comedy", Year = 1984 );
     movies.Add(new { Title = "Gone with Wind", Genre = "Drama", Year = 1939 );
     movies.Add(new { Title = "Star Wars", Genre = "Science Fiction", Year = 1977 });

     return Json(movies, JsonRequestBehavior.AllowGet);
 }

Source : https://msdn.microsoft.com/en-us/library/system.web.mvc.jsonresult(v=vs.118).aspx

来源:https://msdn.microsoft.com/en-us/library/system.web.mvc.jsonresult(v=vs.118).aspx

#2


0  

The MVC Controller class has a Json method which is used to convert an object into a JsonResult. You do not need to manually serialize the object using JavaScriptSerializer. Just pass the object to the Json method, and it will be serialized for you when the result is executed. If your controller method is a GET method, then you will also need to pass JsonRequestBehavior.AllowGet as the second parameter of the Json method, otherwise it will throw an error.

MVC Controller类有一个Json方法,用于将对象转换为JsonResult。您无需使用JavaScriptSerializer手动序列化对象。只需将对象传递给Json方法,它就会在执行结果时为您序列化。如果你的控制器方法是一个GET方法,那么你还需要传递JsonRequestBehavior.AllowGet作为Json方法的第二个参数,否则它将抛出一个错误。

[HttpGet]
public ActionResult GetMyList()
{
    List<object> list = getList();
    return Json(list, JsonRequestBehavior.AllowGet);
}

#1


0  

The following example shows how to return an instance of the JsonResult class from an action method. The object that is returned specifies that a GET request is permitted.

以下示例显示如何从操作方法返回JsonResult类的实例。返回的对象指定允许GET请求。

 public ActionResult Movies()
 {
     var movies = new List<object>();

     movies.Add(new { Title = "Ghostbusters", Genre = "Comedy", Year = 1984 );
     movies.Add(new { Title = "Gone with Wind", Genre = "Drama", Year = 1939 );
     movies.Add(new { Title = "Star Wars", Genre = "Science Fiction", Year = 1977 });

     return Json(movies, JsonRequestBehavior.AllowGet);
 }

Source : https://msdn.microsoft.com/en-us/library/system.web.mvc.jsonresult(v=vs.118).aspx

来源:https://msdn.microsoft.com/en-us/library/system.web.mvc.jsonresult(v=vs.118).aspx

#2


0  

The MVC Controller class has a Json method which is used to convert an object into a JsonResult. You do not need to manually serialize the object using JavaScriptSerializer. Just pass the object to the Json method, and it will be serialized for you when the result is executed. If your controller method is a GET method, then you will also need to pass JsonRequestBehavior.AllowGet as the second parameter of the Json method, otherwise it will throw an error.

MVC Controller类有一个Json方法,用于将对象转换为JsonResult。您无需使用JavaScriptSerializer手动序列化对象。只需将对象传递给Json方法,它就会在执行结果时为您序列化。如果你的控制器方法是一个GET方法,那么你还需要传递JsonRequestBehavior.AllowGet作为Json方法的第二个参数,否则它将抛出一个错误。

[HttpGet]
public ActionResult GetMyList()
{
    List<object> list = getList();
    return Json(list, JsonRequestBehavior.AllowGet);
}