[英]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
,您需要在AvnetCourseType
内CourseTypeContainer
字段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.