Home > Enterprise >  Newtonsoft.JSON null values when deserializing
Newtonsoft.JSON null values when deserializing

Time:12-23

I'm using .NET CORE 3.1 and Newtonsoft.Json to deserialize a JSON response from the API.

this is how the JSON response is structured from the API:

{
    "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" 
            }
        } 
    ] 
} 

This is how I structured the c# class with regards to the response:

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; }
    }

The problem is when I try to deserialize the response JSON it always leads to having null values for 5LevelOrganization:

var fiveLevelResult = _5levelresponse.Content.ReadAsStringAsync().Result;
FiveLevel fivelevel = JsonConvert.DeserializeObject<FiveLevel>(fiveLevelResult);

Image for NULL 5LevelOrganization

Initially I thought the issue was with the JSON response property which starts with a number (5LevelOrganization) so I added the JsonPropertyAttribute but it still won't deserialize properly resulting to NULL. The response from the API is as expected.

I'm just wondering where I did wrong? Any help or information is greatly appreciated.

CodePudding user response:

As what Llama said I was using the wrong attribute for the Serializer that I'm using when I switched [JsonPropertyName] to [JsonProperty] from the JSON.NET library the results is as expected.

CodePudding user response:

[JsonPropertyName] is a System.Text.Json attribute but you are trying to use a Newtonsoft.Json deserialiazer.

You have to change attribute to [JsonProperty] or to use a System.Text.Json deserializer

using System.Text.Json;

FiveLevel fivelevel = JsonSerializer.Deserialize<FiveLevel>(fiveLevelResult);
  • Related