7

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:

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:

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

I receive JSON result like this:

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

and calling http://www.app123.com/api/team

I receive JSON result like this:

{
   "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?

3 Answers 3

10

First, make sure you are using JSON formatter, e.g. adding following code to 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.

[HttpGet]
public HttpResponseMessage GetPeopleList()
{
    var people = // create a list of person here...
    return Request.CreateResponse(HttpStatusCode.OK, people);
}
Sign up to request clarification or add additional context in comments.

1 Comment

do you add that code you pasted within the WebApiConfig.cs?
5

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, Person and Team classes.

1 Comment

this is what I meant in my answer, returning JSON result along with JSON beahavior!
2

you need to return JSON. Try this

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..

2 Comments

let me know if it didn't help you.
Json is not a member of ApiController, its a member of Controller this doesn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.