Home > Backend Development > C++ > How can I gracefully handle missing data when deserializing JSON to C# using JSON.NET?

How can I gracefully handle missing data when deserializing JSON to C# using JSON.NET?

Susan Sarandon
Release: 2025-01-26 16:36:11
Original
959 people have browsed it

How can I gracefully handle missing data when deserializing JSON to C# using JSON.NET?

Use json.net to series to sequencing JSON data to C#: processing missing data elegantly

When processing JSON data in C#, it is important to process the situation where the JSON structure lacks certain attributes defined in the C#class. Traditionally, the method of extracting each value manually from the JSON object is tedious and easy to make errors.

Fortunately, JSON.NET provides an elegant solution. By creating a C#class that reflects the JSON structure you are analyzing, you can use the

method. This method will automatically map the JSON attribute to the corresponding attributes in the class.

JsonConvert.DeserializeObject For example, consider the following JSON data:

and the corresponding C#class:
<code class="language-json">{
    "givenname": [
        "Robert"
    ],
    "passwordexpired": "20091031041550Z",
    "accountstatus": [
        "active"
    ],
    "accountstatusexpiration": [
        "20100612000000Z"
    ],
    "accountstatusexpmaxdate": [
        "20110410000000Z"
    ],
    "accountstatusmodifiedby": {
        "20100214173242Z": "tdecker",
        "20100304003242Z": "jsmith",
        "20100324103242Z": "jsmith",
        "20100325000005Z": "rjones",
        "20100326210634Z": "jsmith",
        "20100326211130Z": "jsmith"
    },
    "accountstatusmodifytimestamp": [
        "20100312001213Z"
    ],
    "affiliation": [
        "Employee",
        "Contractor",
        "Staff"
    ],
    "affiliationmodifytimestamp": [
        "20100312001213Z"
    ],
    "affiliationstatus": [
        "detached"
    ],
    "entrycreatedate": [
        "20000922072747Z"
    ],
    "username": [
        "rjohnson"
    ],
    "primaryaffiliation": [
        "Staff"
    ],
    "employeeid": [
        "999777666"
    ],
    "sn": [
        "Johnson"
    ]
}</code>
Copy after login

Using the Method, you can reclining JSON data into the instance of the
<code class="language-csharp">public class MyAccount
{
    [JsonProperty(PropertyName = "username")]
    public string UserID { get; set; }

    [JsonProperty(PropertyName = "givenname")]
    public string GivenName { get; set; }

    [JsonProperty(PropertyName = "sn")]
    public string Surname { get; set; }

    [JsonProperty(PropertyName = "passwordexpired")]
    public DateTime PasswordExpire { get; set; }

    [JsonProperty(PropertyName = "primaryaffiliation")]
    public string PrimaryAffiliation { get; set; }

    [JsonProperty(PropertyName = "affiliation")]
    public string[] Affiliation { get; set; }

    [JsonProperty(PropertyName = "affiliationstatus")]
    public string AffiliationStatus { get; set; }

    [JsonProperty(PropertyName = "affiliationmodifytimestamp")]
    public DateTime AffiliationLastModified { get; set; }

    [JsonProperty(PropertyName = "employeeid")]
    public string EmployeeID { get; set; }

    [JsonProperty(PropertyName = "accountstatus")]
    public string AccountStatus { get; set; }

    [JsonProperty(PropertyName = "accountstatusexpiration")]
    public DateTime AccountStatusExpiration { get; set; }

    [JsonProperty(PropertyName = "accountstatusexpmaxdate")]
    public DateTime AccountStatusExpirationMaxDate { get; set; }

    [JsonProperty(PropertyName = "accountstatusmodifiedby")]
    public Dictionary<string, string> AccountStatusModifiedBy { get; set; }

    [JsonProperty(PropertyName = "accountstatusmodifytimestamp")]
    public DateTime AccountStatusModified { get; set; }

    [JsonProperty(PropertyName = "accountstatusexpnotice")]
    public string AccountStatusExpNotice { get; set; }

    [JsonProperty(PropertyName = "entrycreatedate")]
    public DateTime EntryCreatedate { get; set; }

    [JsonProperty(PropertyName = "entrydeactivationdate")]
    public DateTime EntryDeactivationDate { get; set; }
}</code>
Copy after login
class:

JsonConvert.DeserializeObject MyAccount This will automatically use the value of

in JSON data.
<code class="language-csharp">var myAccount = JsonConvert.DeserializeObject<MyAccount>(jsonString);</code>
Copy after login

Even if the JSON structure lacks certain attributes, such as myAccount or

, the dependentization process will be carried out smoothly, and the corresponding attributes in class C#will be set to its silent value. This method eliminates the needs of complex conditions or manual value assignment, and simplifies your data conversion process.

The above is the detailed content of How can I gracefully handle missing data when deserializing JSON to C# using JSON.NET?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template