【发布时间】:2022-01-23 19:19:34
【问题描述】:
我正在使用 .NET CORE 3.1 和 Newtonsoft.Json 反序列化来自 API 的 JSON 响应。
这是从 API 构造 JSON 响应的方式:
{ “页面大小”:200, “页数”:1, “记录数”:13, “页码”:1, “结果”:“确定”, “组织”:[ { "5LevelOrganization": { "organizationLevel1Code": "xxxxxxxx", "organizationLevel1Name": "企业人力资源", "organizationLevel2Code": "xxxxxxxx", "organizationLevel2Name": "BHR 下游和中游", "organizationLevel3Code": "xxxxxxxx", "organizationLevel3Name": "化学品", "organizationLevel4Code": "", "organizationLevel4Name": "", "organizationLevel5Code": "xxxxxxxx", "organizationLevel5Name": "化学品" } }, { "5LevelOrganization": { "organizationLevel1Code": "xxxxxxxx", "organizationLevel1Name": "企业人力资源", "organizationLevel2Code": "xxxxxxxx", "organizationLevel2Name": "BHR 下游和中游", "organizationLevel3Code": "xxxxxxxx", "organizationLevel3Name": "化学品", "organizationLevel4Code": "xxxxxxxx", "organizationLevel4Name": "Americas Oronite Manufacturing HR", "organizationLevel5Code": "xxxxxxxx", "organizationLevel5Name": "美洲 Oronite HR Managed" } } ] }这就是我在响应方面构建 c# 类的方式:
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 具有空值:
var fiveLevelResult = _5levelresponse.Content.ReadAsStringAsync().Result;
FiveLevel fivelevel = JsonConvert.DeserializeObject<FiveLevel>(fiveLevelResult);
Image for NULL 5LevelOrganization
最初我认为问题出在以数字 (5LevelOrganization) 开头的 JSON 响应属性上,所以我添加了 JsonPropertyAttribute,但它仍然无法正确反序列化,导致为 NULL。 API 的响应符合预期。
我只是想知道我哪里做错了?非常感谢任何帮助或信息。
【问题讨论】:
-
[JsonPropertyName]是 System.Text.Json 序列化程序的一个属性,但您使用 JSON.NET 作为序列化程序。您需要 JSON.NET 库中的[JsonProperty]。 -
P.S.注意
[.net]标签是指.NET Framework,而你的问题是关于.NET Core,所以你应该使用[.net-core]标签以免误导人。 -
它works fine 当你使用正确的属性时。投票结束是一个错字。
-
@Llama 非常感谢。现在可以正常使用了!