Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC 4 Web API - JSON serialization for custom objects
    text
    copied!<p>I have several objects as followed:</p> <pre><code>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&lt;Person&gt; TeamMembers; public Team(string name, Person lead, List&lt;Person&gt; 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; } } </code></pre> <p>(1) This is the Person controller with Get method:</p> <pre><code>public class PersonController : ApiController { public Response Get() { Person tom = new Person("Tom", "Cruise"); Response response = new Response(1, "It works!", tom); return response; } } </code></pre> <p>(2) This is the Team controller with Get method:</p> <pre><code>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&lt;Person&gt;(){tom,cindy}); Response response = new Response(1, "It works!", awesome); return response; } } </code></pre> <p>What I want is after user calling <a href="http://www.app123.com/api/person">http://www.app123.com/api/person</a></p> <p>I receive JSON result like this:</p> <pre><code>{ "ResponseCode":1, "ResponseMessage":"It works!", "ResponsePayload": { "FirstName":"Tom", "LastName":"Cruise" } } </code></pre> <p>and calling <a href="http://www.app123.com/api/team">http://www.app123.com/api/team</a></p> <p>I receive JSON result like this:</p> <pre><code>{ "ResponseCode":1, "ResponseMessage":"It works!", "ResponsePayload": { "TeamLeader": { "FirstName":"Jason", "LastName":"Lien" } "TeamMember": [ { "FirstName":"Tom", "LastName":"Cruise" }, { "FirstName":"Cindy", "LastName":"Cullen" } ] } } </code></pre> <p>But they never work for me, do you know how to produce the JSON result like above with ASP.NET MVC 4?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload