簡體   English   中英

如何從 web api 中的響應消息中獲取單個列表項

[英]How to get single list item from response message in web api

我是 web api 的新手。 我有一個方法,它從響應消息中返回 3 個對象。 我想從響應消息中獲取特定對象,

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

在上面的代碼中,我想從響應消息中單獨獲取 ListOfStudents 對象。 請任何人幫助我得到這個。

我認為你有一個格式錯誤的 json ,你應該為每個列表創建一個屬性,檢查下一個例子:

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

使用 jquery 獲取此示例

$.get("GetAllStudents", function(data) {
   if (data.success)
   {
      console.log(data.listOfStudents);
   }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM