使用RestSharp客户端反序列化嵌套的JSON响应

我想使用REST Api并反序列化嵌套的JSON响应。 为此,我尝试创建一些代表JSON Response [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 Type { get; set; } } /* each Course has n CourseTypes */ public class CourseType { public string Name { get; set; } public string Title { get; set; } public List 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(request); 

编辑1 :我发现了一个错字,AvnetCourse中的Type属性应该命名为Types

 public List Type { get; set; } // wrong public List 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 :我使用List实现了Course.Types属性,而不是List提出的ListCourseTypeDescriptionContainer也是CourseTypeDescriptionContainer

 public List Type { get; set; } // OLD public List Descriptions { get; set; } // OLD public List Type { get; set; } // NEW public List Descriptions { get; set; } // NEW 

现在, response.Data.Types最终被正确填充。 但是,response.Data.Types.Descriptions仍未正确填充,因为还有一个额外的语言层(例如“EN”)。 如何在不为每种语言创建PACO的情况下解决这个问题?

编辑3 :我必须添加一个额外的CourseTypeDescriptionDetails类,我将存储描述性数据。 在我的CourseTypeDescription中,我为每种语言添加了类型列表的属性。 代码片段:

 public class AvnetCourseType { public List Descriptions { get; set; } // other properties } public class CourseTypeDescription { public List EN { get; set; } // English public List 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 Types { get; set; } } 

现在您有CourseTypeContainer列表。 而且CourseTypeContainer

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

因此,当您尝试获取response.Data.Types[0].AvnetCourseType ,您需要在CourseTypeContainer包含字段CourseTypeContainer

或者我认为你想要的实际是这个公共List Types { get; set; } List Types { get; set; } 你不需要一个容器。