-2

I am new to web api. I have a method, that returns 3 objects from response message. I want to get specific object from the response message,

public HttpResponseMessage GetAllStudents(HttpReqestMessage request)
{
   HttpResponseMessage response = null;
   return CreateHttpResponse(request, () =>
   {
      // some logics here
      response = request.CreateResponse(HttpStatusCode = OK, new {success = true, StudentName, ListOfStudents, ListOfSubjects});

      return response;
   });
}

In this above code i want to get the ListOfStudents object alone from the response message. Please anyone help me to get this.

Yogeshwaran
  • 170
  • 12
  • Are you saying that you want to get just that object here in this code? Or in the code which calls this service? If the latter, where is that code? What format is the response? How do you currently read that response? – David Dec 04 '18 at 14:53
  • This feels like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What does `CreateHttpResponse` do? How are we suppose to know what it will return? – Nkosi Dec 04 '18 at 14:59
  • 1
    try this: `response.Content.ReadAsStringAsync().Result` and you'll get a json string in return. Next try to deserialize the json string with this: https://stackoverflow.com/questions/4611031/convert-json-string-to-c-sharp-object – Jordec Dec 04 '18 at 15:23
  • I guess you could use another `ReadAs...Async()`, but not sure which one. – Jordec Dec 04 '18 at 15:23

1 Answers1

0

I think you have a malformatted json , you should create a property for each list, check the next example :

public HttpResponseMessage GetAllStudents(HttpReqestMessage request)
{
   HttpResponseMessage response = null;
   return CreateHttpResponse(request, () =>
   {
      // some logics here
      response = request.CreateResponse(HttpStatusCode = OK, new {success = true, studentName = StudentName, listOfStudents = ListOfStudents, listOfSubjects =  ListOfSubjects});
      return response;
   });
}

Example getting this with with jquery

$.get("GetAllStudents", function(data) {
   if (data.success)
   {
      console.log(data.listOfStudents);
   }
});
Ivan Valadares
  • 869
  • 1
  • 8
  • 18