首页 > 解决方案 > 将 json 字符串反序列化为桌面应用程序中的 C# 对象

问题描述

{
    "autoRegUUID": null,
    "federationId": "nlk:1601014",
    "identityProvider": {
        "identityProvider": false,
        "identityProviderBusinessKey": "TINC",
        "identityProviderCode": "TINC",
        "identityProviderName": "Prime"
    },
    "loginName": "nlk",
    "primaryTenant": {
        "orgAddress": "13034 Ballne Corporate Pla",
        "orgCity": "Charlotte",
        "orgName": "Prime, Inc.",
        "orgState": "NC",
        "orgZip": "28277-198",
        "primaryOrganization": "NC0",
        "tin": null
    },
    "puid": "rreppsSsAPnPBgt",
    "userId": "rrepp",
    "userProfile": {
        "activeUser": true,
        "email": "Neil_Kirk@Prime.com",
        "firstName": "Neil",
        "internalUser": true,
        "jobTitle": "Portfolio Business Analyst",
        "lastLogin": "Jul 01, 2020, 15:23:14 PM",
        "lastName": "Kirk",
        "npi": null,
        "workPhone": "+14123995262",
        "workPhoneExt": null
    }
}

这是上课

public class PremierUserDetails
    {
        public string autoRegUUID { get; set; }

        public string federationId { get; set; }

        public string loginName { get; set; }

        public string puid { get; set; }

        public string userId { get; set; }

        public List<identityProvider> Providers { get; set; }

        public List<primaryTenant> Tenant { get; set; }

        public List<userProfile> Profile { get; set; }
    }

    public class identityProvider 
    {
        public bool identityProviderAuthViaSSO { get; set; }
        public string identityProviderBusinessKey { get; set; }
        public string identityProviderCode { get; set; }
        public string identityProviderName { get; set; }

    }

    public class primaryTenant 
    {
        public string orgAddress { get; set; }
        public string orgCity { get; set; }
        public string orgName { get; set; }
        public string orgState { get; set; }
        public string orgZip { get; set; }
        public string primaryOrganization { get; set; }
        public string tin { get; set; }

    }

    public class userProfile 
    {
        public bool activeUser { get; set; }
        public string email { get; set; }
        public string firstName { get; set; }
        public string displayName { get; set; }
        public string internalUser { get; set; }
        public string jobTitle { get; set; }
        public string lastLogin { get; set; }
        public string lastName { get; set; }
        public string npi { get; set; }
        public string workPhone { get; set; }
        public string workPhoneExt { get; set; }
    }

..

var jsonData = JsonConvert.DeserializeObject<PremierUserDetails>(json);
        PremierUserDetails user = new PremierUserDetails();

但是 Providers、Tenant 和 Profile 都是空余的,它们都填充了值。

标签: c#jsonlinq

解决方案


您的“子”对象...不要/不要似乎是 json 数组。(一个赠品是缺少 [] 方括号)

尝试:

public class PremierUserDetails
    {
        public string autoRegUUID { get; set; }

        public string federationId { get; set; }

        public string loginName { get; set; }

        public string puid { get; set; }

        public string userId { get; set; }

        public identityProvider identityProvider{ get; set; }

        public primaryTenant primaryTenant { get; set; }

        public userProfile userProfile { get; set; }
    }

现在,

https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-classes-structs-and-interfaces

C# 类名是 PascalCase,.. 所以你想以不同的方式处理你的命名约定。

你可以在这里看到如何做到这一点:

https://www.newtonsoft.com/json/help/html/JsonPropertyName.htm


推荐阅读