在 C# 中反序列化复杂的 JSON 对象

作者 : 慕源网 本文共1802个字,预计阅读时间需要5分钟 发布时间: 2022-03-3 共398人阅读

介绍

使用 C# 中的 Newtonsoft.Json NuGet 包在 C# 中序列化和反序列化 JSON 对象非常简单。在这篇博客中,我将解释如何反序列化复杂对象。 

处理复杂的 JSON

假设我们有一个复杂的 JSON 对象,如下所示,

{
    "value": [{
        "id": "/subscriptions/abc247d5-724f-4286-9630-d517572af93f",
        "authorizationSource": "RoleBased",
        "managedByTenants": [],
        "tags": {
            "Department": "Dev",
            "Account": "Sales",
            "Division": "001",
            "CostCenter": "003",
            "test": "jhjh"
        },
        "subscriptionId": "abc247d5-724f-4286-9630-d517572af93f",
        "tenantId": "020a0840-77ca-4d29-9a85-7cf2853d0e98",
        "displayName": "Sample",
        "state": "Enabled",
        "subscriptionPolicies": {
            "locationPlacementId": "PublicAndIndia_2015-09-01",
            "quotaId": "2014-09-01",
            "spendingLimit": "On"
        }
    }, {
        "id": "/subscriptions/abc247d5-724f-4286-9630-d517572af93d",
        "authorizationSource": "RoleBased",
        "managedByTenants": [],
        "tags": {
            "Dept": "Dev"
        },
        "subscriptionId": "abc247d5-724f-4286-9630-d517572af93d",
        "tenantId": "310a0840-77ca-4d29-9a85-7cf2853d0e98",
        "displayName": "Sample Subscription",
        "state": "Enabled",
        "subscriptionPolicies": {
            "locationPlacementId": "PublicAndIndia_2015-09-01",
            "quotaId": "2016-01-01",
            "spendingLimit": "Off"
        }
    }],
    "count": {
        "type": "Total",
        "value": 2
    }
}

这可以使用 Newtonsoft.json 轻松反序列化

首先通过创建模型类分解所有实体

RootObject.cs

public class RootObject {
    public List < Value > value {
        get;
        set;
    }
    public Count count {
        get;
        set;
    }
}

Value.cs

public class Value {
    public string id {
        get;
        set;
    }
    public string authorizationSource {
        get;
        set;
    }
    public string[] managedByTenants {
        get;
        set;
    }
    public Dictionary < string, string > tags {
        get;
        set;
    }
    public string subscriptionId {
        get;
        set;
    }
    public string tenantId {
        get;
        set;
    }
    public string displayName {
        get;
        set;
    }
    public string state {
        get;
        set;
    }
    public SubscriptionPolicies SubscriptionPolicies {
        get;
        set;
    }
}

SubscriptionPolicies.cs

public class SubscriptionPolicies {
    public string locationPlacementId {
        get;
        set;
    }
    public string quotaId {
        get;
        set;
    }
    public string spendingLimit {
        get;
        set;
    }
}

Count.cs

public class Count {
    public string type {
        get;
        set;
    }
    public int value {
        get;
        set;
    }
}

反序列化复杂 JSON 字符串的代码,

反序列化后,


慕源网 » 在 C# 中反序列化复杂的 JSON 对象

常见问题FAQ

程序仅供学习研究,请勿用于非法用途,不得违反国家法律,否则后果自负,一切法律责任与本站无关。
请仔细阅读以上条款再购买,拍下即代表同意条款并遵守约定,谢谢大家支持理解!

发表评论

开通VIP 享更多特权,建议使用QQ登录