[英]Why are all the collections in my POCO are null when deserializing some valid json with the .NET Newtonsoft.Json component
[英]Newtonsoft.JSON null values when deserializing
我正在使用 .NET CORE 3.1 和 Newtonsoft.Json 反序列化来自 JSON 响应的 JSON 响应来自 ZADB97423887041
这是从 API 构造 JSON 响应的方式:
{ "PageSize": 200, "PageCount": 1, "RecordCount": 13, "PageNumber": 1, "Result": "OK", "organization":[ { "5LevelOrganization": { "organizationLevel1Code": "xxxxxxxx", "organizationLevel1Name": "Corporate Human Resources", "organizationLevel2Code": "xxxxxxxx", "organizationLevel2Name": "BHR Downstream & Midstream", "organizationLevel3Code": "xxxxxxxx", "organizationLevel3Name": "Chemicals", "organizationLevel4Code": "", "organizationLevel4Name": "", "organizationLevel5Code": "xxxxxxxx", "organizationLevel5Name": "Chemicals" } }, { "5LevelOrganization": { "organizationLevel1Code": "xxxxxxxx", "organizationLevel1Name": "Corporate Human Resources", "organizationLevel2Code": "xxxxxxxx", "organizationLevel2Name": "BHR Downstream & Midstream", "organizationLevel3Code": "xxxxxxxx", "organizationLevel3Name": "Chemicals", "organizationLevel4Code": "xxxxxxxx", "organizationLevel4Name": "Americas Oronite Manufacturing HR", "organizationLevel5Code": "xxxxxxxx", "organizationLevel5Name": "Americas Oronite HR Managed" } } ] }
这就是我关于响应构建 c# class 的方式:
public class _5LevelOrganization
{
[JsonPropertyName("organizationLevel1Code")]
public string OrganizationLevel1Code { get; set; }
[JsonPropertyName("organizationLevel1Name")]
public string OrganizationLevel1Name { get; set; }
[JsonPropertyName("organizationLevel2Code")]
public string OrganizationLevel2Code { get; set; }
[JsonPropertyName("organizationLevel2Name")]
public string OrganizationLevel2Name { get; set; }
[JsonPropertyName("organizationLevel3Code")]
public string OrganizationLevel3Code { get; set; }
[JsonPropertyName("organizationLevel3Name")]
public string OrganizationLevel3Name { get; set; }
[JsonPropertyName("organizationLevel4Code")]
public string OrganizationLevel4Code { get; set; }
[JsonPropertyName("organizationLevel4Name")]
public string OrganizationLevel4Name { get; set; }
[JsonPropertyName("organizationLevel5Code")]
public string OrganizationLevel5Code { get; set; }
[JsonPropertyName("organizationLevel5Name")]
public string OrganizationLevel5Name { get; set; }
}
public class Organization
{
[JsonPropertyName("5LevelOrganization")]
public _5LevelOrganization _5LevelOrganization { get; set; }
}
public class FiveLevel
{
[JsonPropertyName("PageSize")]
public int PageSize { get; set; }
[JsonPropertyName("PageCount")]
public int PageCount { get; set; }
[JsonPropertyName("RecordCount")]
public int RecordCount { get; set; }
[JsonPropertyName("PageNumber")]
public int PageNumber { get; set; }
[JsonPropertyName("Result")]
public string Result { get; set; }
[JsonPropertyName("organization")]
public List<Organization> Organization { get; set; }
}
问题是当我尝试反序列化响应 JSON 时,它总是导致 5LevelOrganization 的 null 值:
var fiveLevelResult = _5levelresponse.Content.ReadAsStringAsync().Result;
FiveLevel fivelevel = JsonConvert.DeserializeObject<FiveLevel>(fiveLevelResult);
最初我认为问题出在 JSON 响应属性上,它以数字(5LevelOrganization)开头,所以我添加了 JsonPropertyAttribute 但它仍然无法正确反序列化,导致 NULL。 API 的响应与预期一致。
我只是想知道我哪里做错了? 非常感谢任何帮助或信息。
正如 Llama 所说,当我从 JSON.NET 库切换[JsonPropertyName]
到[JsonProperty]
时,我使用的序列化程序的属性错误,结果与预期一致。
[JsonPropertyName] 是 System.Text.Json 属性,但您正在尝试使用 Newtonsoft.Json 解串器。
您必须将属性更改为 [JsonProperty] 或使用 System.Text.Json 反序列化器
using System.Text.Json;
FiveLevel fivelevel = JsonSerializer.Deserialize<FiveLevel>(fiveLevelResult);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.