7

我有几个对象如下:

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) 这是带有 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) 这是带有 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;
    }
}

我想要的是在用户调用 http://www.app123.com/api/person之后

我收到这样的 JSON 结果:

{
   "ResponseCode":1,
   "ResponseMessage":"It works!",
   "ResponsePayload":
   {
     "FirstName":"Tom",
     "LastName":"Cruise"
   } 
}

并致电 http://www.app123.com/api/team

我收到这样的 JSON 结果:

{
   "ResponseCode":1,
   "ResponseMessage":"It works!",
   "ResponsePayload":
   {
     "TeamLeader":
      {
          "FirstName":"Jason",
          "LastName":"Lien"
      }
      "TeamMember":
      [
         {
            "FirstName":"Tom",
            "LastName":"Cruise"
         },
         {
             "FirstName":"Cindy",
             "LastName":"Cullen"
         }
      ]
   } 
}

但他们从来没有为我工作,你知道如何使用 ASP.NET MVC 4 生成上面的 JSON 结果吗?

4

3 回答 3

10

首先,确保您使用的是 JSON 格式化程序,例如将以下代码添加到 Application_Start

var json = config.Formatters.JsonFormatter;
json.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;

其次,只需返回您的自定义对象,JSON 格式化程序将完成剩下的工作,您将在客户端获得漂亮的 JSON 数据。

[HttpGet]
public HttpResponseMessage GetPeopleList()
{
    var people = // create a list of person here...
    return Request.CreateResponse(HttpStatusCode.OK, people);
}
于 2013-06-13T07:35:52.967 回答
5

这个对我有用:

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;
}

我们还需要为Response、PersonTeam类注解[Serializable] 。

于 2013-04-04T14:30:28.933 回答
2

您需要返回 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);
}
}

并在其他控制器方法中也遵循相同的方法..

于 2013-04-04T06:05:25.397 回答