MVC 4 Web API - 自定义对象的 JSON 序列化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15803583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC 4 Web API - JSON serialization for custom objects
提问by Tuyen Nguyen
I have several objects as followed:
我有几个对象如下:
public class Person
{
string FirstName;
string LastName;
public Person(string fn, string ln)
{
FirstName = fn;
LastName = ln;
}
}
public class Team
{
string TeamName;
Person TeamLeader;
List<Person> TeamMembers;
public Team(string name, Person lead, List<Person> members)
{
TeamName = name;
TeamLeader = lead;
TeamMembers = members;
}
}
public class Response
{
int ResponseCode;
string ResponseMessage;
object ResponsePayload;
public Response(int code, string message, object payload)
{
ResponseCode = code;
ResponseMessage = message;
ResponsePayload = payload;
}
}
(1) This is the Person controller with Get method:
(1) 这是带有 Get 方法的 Person 控制器:
public class PersonController : ApiController
{
public Response Get()
{
Person tom = new Person("Tom", "Cruise");
Response response = new Response(1, "It works!", tom);
return response;
}
}
(2) This is the Team controller with Get method:
(2) 这是带有 Get 方法的 Team 控制器:
public class TeamController : ApiController
{
public Response Get()
{
Person tom = new Person("Tom", "Cruise");
Person cindy = new Person("Cindy", "Cullen");
Person jason = new Person("Jason","Lien");
Team awesome = new Team("Awesome", jason, new List<Person>(){tom,cindy});
Response response = new Response(1, "It works!", awesome);
return response;
}
}
What I want is after user calling http://www.app123.com/api/person
我想要的是在用户调用 http://www.app123.com/api/person 之后
I receive JSON result like this:
我收到这样的 JSON 结果:
{
"ResponseCode":1,
"ResponseMessage":"It works!",
"ResponsePayload":
{
"FirstName":"Tom",
"LastName":"Cruise"
}
}
and calling http://www.app123.com/api/team
并调用 http://www.app123.com/api/team
I receive JSON result like this:
我收到这样的 JSON 结果:
{
"ResponseCode":1,
"ResponseMessage":"It works!",
"ResponsePayload":
{
"TeamLeader":
{
"FirstName":"Jason",
"LastName":"Lien"
}
"TeamMember":
[
{
"FirstName":"Tom",
"LastName":"Cruise"
},
{
"FirstName":"Cindy",
"LastName":"Cullen"
}
]
}
}
But they never work for me, do you know how to produce the JSON result like above with ASP.NET MVC 4?
但他们从来没有对我来说有效,你知道如何用 ASP.NET MVC 4 产生像上面那样的 JSON 结果吗?
采纳答案by Tuyen Nguyen
This one works for me:
这个对我有用:
public object Get()
{
Person tom = new Person("Tom", "Cruise");
Person cindy = new Person("Cindy", "Cullen");
Person jason = new Person("Jason", "Lien");
Team awesome = new Team("Awesome", jason, new List<Person>() { tom, cindy });
Response response = new Response(1, "It works!", awesome);
JsonResult jsonResult = new JsonResult {
Data= response,
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
return jsonResult.Data;
}
We also need to anotate [Serializable]for Response, Personand Teamclasses.
我们还需要为Response、Person和Team类注释[Serializable]。
回答by Tien Do
First, make sure you are using JSON formatter, e.g. adding following code to Application_Start
首先,确保您使用的是 JSON 格式化程序,例如将以下代码添加到 Application_Start
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
Second, just returning your custom object, JSON formatter will do the rest and you'll get nice JSON data on the client side.
其次,只需返回您的自定义对象,JSON 格式化程序将完成剩下的工作,您将在客户端获得不错的 JSON 数据。
[HttpGet]
public HttpResponseMessage GetPeopleList()
{
var people = // create a list of person here...
return Request.CreateResponse(HttpStatusCode.OK, people);
}
回答by Sandy
you need to return JSON. Try this
你需要返回 JSON。尝试这个
public class PersonController : ApiController
{
public Response Get()
{
Person tom = new Person("Tom", "Cruise");
Response response = new Response(1, "It works!", tom);
return Json(response, JsonRequestBehavior.AllowGet);
}
}
and follow same in other controller method too..
并在其他控制器方法中也遵循相同的方法..

