簡體   English   中英

使用 RestSharp 客戶端反序列化嵌套的 JSON 響應

[英]Deserialize nested JSON Response with RestSharp Client

我想使用 REST Api 並反序列化嵌套的 JSON 響應。 為此,我嘗試創建一些代表 JSON 響應 [1] 的 POCO 類。 響應如下所示:

{
  "success": true,
  "message": "OK",
  "types": 
  [
    {
      "name": "A5EF3-ASR",
      "title": "ITIL Foundation Plus Cloud Introduction",
      "classroomDeliveryMethod": "Self-paced Virtual Class",
      "descriptions": {
        "EN": {
          "description": "some Text null",
          "overview": null,
          "abstract": "Some other text",
          "prerequisits": null,
          "objective": null,
          "topic": null
        }
      },
      "lastModified": "2014-10-08T08:37:43Z",
      "created": "2014-04-28T11:23:12Z"
    },
    {
      "name": "A4DT3-ASR",
      "title": "ITIL Foundation eLearning Course + Exam",
      "classroomDeliveryMethod": "Self-paced Virtual Class",
      "descriptions": {
        "EN": {
          "description": "some Text"
          (...)

所以我創建了以下 POCO 類:

public class Course
{
    public bool success { get; set; }
    public string Message { get; set; }
    public List<CourseTypeContainer> Type { get; set; }
}

/* each Course has n CourseTypes */
public class CourseType
{
    public string Name { get; set; }
    public string Title { get; set; }
    public List<CourseTypeDescriptionContainer> Descriptions { get; set; }
    public DateTime LastModified { get; set; }
    public DateTime Created { get; set; }
}
public class CourseTypeContainer
{
    public CourseType CourseType { get; set; }
}


/* each CourseType has n CourseTypeDescriptions */
public class CourseTypeDescription
{
    public string Description { get; set; }
    public string Overview { get; set; }
    public string Abstract { get; set; }
    public string Prerequisits { get; set; }
    public string Objective { get; set; }
    public string Topic { get; set; }
}
public class CourseTypeDescriptionContainer
{
    public CourseTypeDescription CourseTypeDescription { get; set; }
}

這是 API 代碼:

var client = new RestClient("https://www.someurl.com");
client.Authenticator = new HttpBasicAuthenticator("user", "password");

var request = new RestRequest();
request.Resource = "api/v1.0/types";
request.Method = Method.GET;
request.RequestFormat = DataFormat.Json;

var response = client.Execute<Course>(request);

編輯 1 :我發現了一個錯字,AvnetCourse 中的Type屬性應該命名為Types

public List<AvnetCourseTypeContainer> Type { get; set; }    // wrong
public List<AvnetCourseTypeContainer> Types { get; set; }   // correct

現在返回值如下所示:

response.Data.success = true                    // CORRECT
repsonse.Data.Message = "OK"                    // CORRECT
response.Data.Types = (Count: 1234);            // CORRECT
response.Data.Types[0].AvnetCourseType = null;  // NOT CORRECT

編輯2:我實現了Course.Types使用Property List<CourseType> ,而不是一個List<CourseTypeContainer>所建議的Jaanus。 CourseTypeDescriptionContainer也是CourseTypeDescriptionContainer

public List<CourseTypeContainer> Type { get; set; }                     // OLD
public List<CourseTypeDescriptionContainer> Descriptions { get; set; }  // OLD
public List<CourseType> Type { get; set; }                              // NEW
public List<CourseTypeDescription> Descriptions { get; set; }           // NEW

現在response.Data.Types終於被正確填充了。 然而, response.Data.Types.Descriptions 仍然沒有正確填寫,因為有一個額外的語言層(例如“EN”)。 如果不為每種語言創建 PACO,我該如何解決這個問題?

編輯 3 :我必須添加一個額外的 CourseTypeDescriptionDetails 類,我將在其中存儲描述性數據。 在我的 CourseTypeDescription 中,我為每種語言添加了類型列表的屬性。 代碼片段:

public class AvnetCourseType
{
    public List<CourseTypeDescription> Descriptions { get; set; }
    // other properties
}
public class CourseTypeDescription
{
    public List<CourseTypeDescriptionDetails> EN { get; set; } // English
    public List<CourseTypeDescriptionDetails> NL { get; set; } // Dutch
}
public class CourseTypeDescriptionDetails
{
    public string Description { get; set; }
    public string Overview { get; set; }
    public string Abstract { get; set; }
    public string Prerequisits { get; set; }
    public string Objective { get; set; }
    public string Topic { get; set; }
}

它現在可以工作,但我需要為每種語言向 CourseTypeDescription 添加另一個屬性。

OLD:返回值是

response.Data.success = true            // CORRECT
repsonse.Data.Message = "OK"            // CORRECT
response.Data.Type = null;              // WHY?

那么為什么我的 response.Type 等於 null? 我究竟做錯了什么?

謝謝

資源:[1] 使用 JSON 數組進行 RestSharp 反序列化

嘗試將其用作 POCO:

public class Course
{
    public bool success { get; set; }
    public string message { get; set; }
    public List<CourseTypeContainer> Types { get; set; }
}

現在您有了CourseTypeContainer列表。 CourseTypeContainer

public class CourseTypeContainer
{
    public CourseType CourseType { get; set; }
}

因此,當您嘗試獲取response.Data.Types[0].AvnetCourseType ,您需要在AvnetCourseTypeCourseTypeContainer字段CourseTypeContainer

或者我認為你想要的實際上是這個 public List<CourseType> Types { get; set; } List<CourseType> Types { get; set; } List<CourseType> Types { get; set; } ,你並不需要一個容器存在。

以防萬一這對其他人有幫助,我在這里嘗試了所有方法,但仍然無法在當前版本的 RestSharp (106.6.2) 上運行。 就我所知,RestSharp 完全忽略了 RootElement 屬性,即使它位於頂層。 我的解決方法是手動告訴它提取嵌套的 JSON,然后進行轉換。 我使用 JSON.Net 來實現這一點。

var response = restClient.Execute<T>(restRequest);
response.Content = JObject.Parse(response.Content)[restRequest.RootElement].ToString();
return new JsonDeserializer().Deserialize<T>(response);

我使用http://json2csharp.com/從 JSON 創建 C# 類。 然后,將 RootObject 重命名為我正在創建的模型文件的 ClassName 在 RestSharp Deserializitaion 之后可以訪問嵌套 json 中的所有數據,類似於 responseBody.data.Subject.Alias 其中數據、主題和別名是收到的響應 JSON 中的嵌套節點.

暫無
暫無

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

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