2013-04-04 18 views
7

Ho diversi oggetti come segue:MVC 4 Web API - serializzazione JSON per oggetti personalizzati

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) Questo è il controller Persona con metodo Get:

public class PersonController : ApiController 
{ 
    public Response Get() 
    { 
     Person tom = new Person("Tom", "Cruise"); 
     Response response = new Response(1, "It works!", tom); 
     return response; 
    } 
} 

(2) Questo è il controller della squadra con metodo Get:

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

Quello che voglio è a poppa utente er chiamando http://www.app123.com/api/person

ricevo risultato JSON come questo:

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

e chiamando http://www.app123.com/api/team

ricevo risultato JSON come questo:

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

Ma non hanno mai lavoro per io, sai come produrre il risultato JSON come sopra con ASP.NET MVC 4?

risposta

5

Questo funziona per 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; 
} 

abbiamo anche bisogno di anotate [Serializable] per Response, Le classi e della squadra.

+1

questo è ciò che intendevo nella mia risposta, restituendo il risultato JSON insieme al comportamento di JSON! – Sandy

2

è necessario restituire JSON. Prova questo

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

e mantenere gli stessi in un altro metodo di controllo troppo ..

+0

fammi sapere se non ti ha aiutato. – Sandy

+0

no, non funziona –

+0

Json non è un membro di ApiController, è un membro di Controller che non funziona. – mgrenier

10

Innanzitutto, assicurarsi di utilizzare il formattatore JSON, ad es. l'aggiunta di codice seguente per Application_Start

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

In secondo luogo, proprio restituzione del vostro oggetto personalizzato, JSON formattatore farà il resto e si otterrà dati JSON belle sul lato client.

[HttpGet] 
public HttpResponseMessage GetPeopleList() 
{ 
    var people = // create a list of person here... 
    return Request.CreateResponse(HttpStatusCode.OK, people); 
} 
+0

aggiungi quel codice incollato all'interno di WebApiConfig.cs? –

Problemi correlati