【问题标题】:Newtonsoft.JSON null values when deserializing反序列化时的 Newtonsoft.JSON 空值
【发布时间】: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 非常感谢。现在可以正常使用了!

标签: c# json .net-core


【解决方案1】:

正如 Llama 所说,当我从 JSON.NET 库将 [JsonPropertyName] 切换为 [JsonProperty] 时,我使用的序列化器属性错误,结果符合预期。

【讨论】:

    【解决方案2】:

    [JsonPropertyName] 是 System.Text.Json 属性,但您正在尝试使用 Newtonsoft.Json 反序列化器。

    您必须将属性更改为 [JsonProperty] 或使用 System.Text.Json 反序列化器

    using System.Text.Json;
    
    FiveLevel fivelevel = JsonSerializer.Deserialize<FiveLevel>(fiveLevelResult);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-02
      • 2020-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多