首页 > 后端开发 > C++ > 使用 JSON.NET 将 JSON 反序列化为 C# 时,如何优雅地处理丢失的数据?

使用 JSON.NET 将 JSON 反序列化为 C# 时,如何优雅地处理丢失的数据?

Susan Sarandon
发布: 2025-01-26 16:36:11
原创
959 人浏览过

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

使用JSON.NET将JSON数据反序列化到C#:优雅地处理缺失数据

在C#中处理JSON数据时,至关重要的是要处理JSON结构缺少C#类中定义的某些属性的情况。传统上,手动从JSON对象中提取每个值的方法既繁琐又容易出错。

幸运的是,JSON.NET提供了一种优雅的解决方案。通过创建一个反映您正在解析的JSON结构的C#类,您可以利用JsonConvert.DeserializeObject方法。此方法会自动将JSON属性映射到类中相应的属性。

例如,考虑以下JSON数据:

<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>
登录后复制

以及相应的C#类:

<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>
登录后复制

使用JsonConvert.DeserializeObject方法,您可以将JSON数据反序列化到MyAccount类的实例中:

<code class="language-csharp">var myAccount = JsonConvert.DeserializeObject<MyAccount>(jsonString);</code>
登录后复制

这将自动使用JSON数据中的值填充myAccount的属性。

即使JSON结构缺少某些属性,例如AccountStatusExpNoticeEntryDeactivationDate,反序列化过程也会顺利进行,并且C#类中相应的属性将设置为其默认值。这种方法消除了对复杂条件检查或手动值赋值的需求,简化了您的数据转换过程。

以上是使用 JSON.NET 将 JSON 反序列化为 C# 时,如何优雅地处理丢失的数据?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板