加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用JSON.NET将JSON数据反序列化为C#

发布时间:2020-12-16 18:47:17 所属栏目:百科 来源:网络整理
导读:我是相对新的工作与C#和JSON数据,并寻求指导。我使用C#3.0,与.NET3.5SP1和JSON.NET 3.5r6。 我有一个定义的C#类,我需要从一个JSON结构填充。但是,并非从Web服务检索的条目的每个JSON结构都包含在C#类中定义的所有可能的属性。 我一直在做的似乎是错误的
我是相对新的工作与C#和JSON数据,并寻求指导。我使用C#3.0,与.NET3.5SP1和JSON.NET 3.5r6。

我有一个定义的C#类,我需要从一个JSON结构填充。但是,并非从Web服务检索的条目的每个JSON结构都包含在C#类中定义的所有可能的属性。

我一直在做的似乎是错误的,艰难的方式,只是从JObject中逐个挑选每个值,并将字符串转换为所需的类属性。

JsonSerializer serializer = new JsonSerializer();
var o = (JObject)serializer.Deserialize(myjsondata);

MyAccount.EmployeeID = (string)o["employeeid"][0];

将JSON结构反序列化为C#类并处理JSON源中可能缺失的数据的最佳方法是什么?

我的类被定义为:

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 = "accountstatusmodifytimestamp")]
    public DateTime AccountStatusModified { get; set; }

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

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

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

    [JsonProperty(PropertyName = "entrydeactivationdate")]
    public DateTime EntryDeactivationDate { get; set; }

  }

并且要解析的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"
    ]
}
使用JsonConvert.DeserializeObject< RootObject>(string json);

在JSON 2 C#上创建您的课程

Json.NET文档:Serializing and Deserializing JSON with Json.NET

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读