C# asp.net MVC中的JSON序列化
在我的示例asp.NETMVC应用程序中,当任何用户从浏览器发出AJAX调用时,我都希望以下面提到的JSON格式返回数据C# asp.net MVC中的JSON序列化,c#,ajax,asp.net-mvc,C#,Ajax,Asp.net Mvc,在我的示例asp.NETMVC应用程序中,当任何用户从浏览器发出AJAX调用时,我都希望以下面提到的JSON格式返回数据 { "success":true, // true or false "errorMessage":"", // errorMessage if there is any error "data": { } //this value will depend on the action method called } 为了以这种格式返回响应,我在所有
{
"success":true, // true or false
"errorMessage":"", // errorMessage if there is any error
"data": { } //this value will depend on the action method called
}
为了以这种格式返回响应,我在所有操作方法中创建了一个匿名类型,然后将其序列化为JSON
public ActionResult GetAuthors()
{
List<BookStoreAdmin.ViewModels.Author> authors = BookStoreAdmin.BAL.Author.GetAuthors();
var response = new { success = true, errors = "", data = authors };
return Json(response, JsonRequestBehavior.AllowGet);
// return Json(authors, JsonRequestBehavior.AllowGet);
}
public ActionResult GetAuthors()
{
List authors=BookStoreAdmin.BAL.Author.GetAuthors();
var response=new{success=true,errors=,data=authors};
返回Json(response,JsonRequestBehavior.AllowGet);
//返回Json(authors,JsonRequestBehavior.AllowGet);
}
现在,我的问题是,有没有更好的方法或正确的方法来实现这一点,而不是在所有操作方法中创建匿名类型?一个简单的方法将其保留在一个位置如何
public static class JsonExtensions
{
public static ActionResult ToJson(this object items)
{
var response = new { success = true, errors = "", data = items };
return Json(response, JsonRequestBehavior.AllowGet);
}
}
后来
public ActionResult GetAuthors()
{
List<BookStoreAdmin.ViewModels.Author> authors = BookStoreAdmin.BAL.Author.GetAuthors();
return authors.ToJson();
}
public ActionResult GetAuthors()
{
List authors=BookStoreAdmin.BAL.Author.GetAuthors();
返回authors.ToJson();
}
另外,要么将附加参数传递给方法,要么根据用例创建它们(成功/错误/etc)您可以创建一个泛型模型类,并使用
JsonPropertyAttribute
装饰其属性
例如:
public class Response<T>
{
[JsonProperty("success")]
public bool Success { get; set; }
[JsonProperty("errorMessage")]
public string ErrorMessage { get; set; }
[JsonProperty("data")]
public T Data { get; set; }
}
公共类响应
{
[JsonProperty(“成功”)]
公共bool成功{get;set;}
[JsonProperty(“errorMessage”)]
公共字符串错误消息{get;set;}
[JsonProperty(“数据”)]
公共T数据{get;set;}
}
然后您可以使用它来代替匿名对象。如果您创建一个类来返回响应,您可以获得更好的控制。如果使用类,则可以确定结构和属性名称等 在您的情况下,如果
数据
属性可以包含不同类型的集合,那么您可以创建一个泛型类,如下所示
public class Response<T>
{
public bool Success {get;set;}
public string ErrorMessage {get;set;}
public T Data {get;set;}
}
var response = new Response<List <BookStoreAdmin.ViewModels.Author>>();
response.Success = true;
response.ErrorMessage = "";
response.Data = authors;
return Json(response, JsonRequestBehavior.AllowGet);
公共类响应
{
公共bool成功{get;set;}
公共字符串错误消息{get;set;}
公共T数据{get;set;}
}
并使用它如下
public class Response<T>
{
public bool Success {get;set;}
public string ErrorMessage {get;set;}
public T Data {get;set;}
}
var response = new Response<List <BookStoreAdmin.ViewModels.Author>>();
response.Success = true;
response.ErrorMessage = "";
response.Data = authors;
return Json(response, JsonRequestBehavior.AllowGet);
var response=newresponse();
回答:成功=正确;
response.ErrorMessage=“”;
回答:数据=作者;
返回Json(response,JsonRequestBehavior.AllowGet);
通过这种方式,您可以使用该类返回数据属性中任何类型集合的resposne