मैं पीछा के रूप में कई वस्तुओं है:MVC 4 जाल एपीआई - कस्टम के लिए JSON क्रमबद्धता वस्तुओं
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) यह प्राप्त विधि के साथ व्यक्ति नियंत्रक है:
public class PersonController : ApiController
{
public Response Get()
{
Person tom = new Person("Tom", "Cruise");
Response response = new Response(1, "It works!", tom);
return response;
}
}
(2)
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"
}
]
}
}
लेकिन वे के लिए काम कभी नहीं मैं, क्या आप एएसपी.नेट एमवीसी 4 के साथ जेएसओएन परिणाम उत्पन्न करने के बारे में जानते हैं?
जेएसओएन बीहॉयर के साथ जेएसओएन परिणाम लौटने पर मेरा जवाब यही है! – Sandy