c# - 将 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 都是空余的,它们都填充了值。
解决方案
您的“子”对象...不要/不要似乎是 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; }
}
现在,
C# 类名是 PascalCase,.. 所以你想以不同的方式处理你的命名约定。
你可以在这里看到如何做到这一点:
https://www.newtonsoft.com/json/help/html/JsonPropertyName.htm
推荐阅读
- python - 属性错误:“numpy.ndarray”对象没有属性“value_counts”
- python - Selenium 在切换窗口后找不到存在的元素
- tcl - 如何在 tcl 中搜索许多文本中的某些行
- hyperledger-fabric - 如何查看不匹配的提案响应?
- r - 在R中运行线性回归时如何删除一行数据?
- mysql-8.0 - 为什么mysql8.0中查询表的数据长度不变
- c++ - 无法使用矢量存储数字
- python - GCP - 使用 Pulumi Python 将生命周期规则分配给存储桶时出错
- flutter - 颤振时相机旋转错误
- python - CycleGAN 只产生黑色图像