使用JSON数据反序列化到C#JSON.NET [英] Deserializing JSON data to C# using JSON.NET

查看:168
本文介绍了使用JSON数据反序列化到C#JSON.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是比较新的C#和JSON数据工作,并正在寻求指导。我使用C#3.0,具有.NET3.5SP1和JSON.NET 3.5r6。

我有一个定义的C#类,我需要从一个JSON结构来填充。然而,不是每个JSON结构为从所述web服务检索的条目包含被C#的类中定义的所有可能的属性。

我一直是在做什么似乎是错误的,艰辛的道路,只是一个从JObject挑选出每个值之一,并转化串入所需的类属性。

  JsonSerializer串=新JsonSerializer();
VAR O =(JObject)serializer.Deserialize(myjsondata);MyAccount.EmployeeID =(串)O [雇员] [0];

什么是反序列化的JSON结构到C#类的最佳途径从JSON源处理可能丢失的数据?

我的类定义为:

 公共类我的帐户
  {    [JsonProperty(属性名=用户名)]
    公共字符串用户名{获得;组; }    [JsonProperty(属性名=给定名称)]
    公共字符串给定名称{搞定;组; }    [JsonProperty(属性名=SN)]
    公共字符串姓氏{搞定;组; }    [JsonProperty(属性名=passwordexpired)]
    公众的DateTime PasswordExpire {搞定;组; }    [JsonProperty(属性名=primaryaffiliation)]
    公共字符串PrimaryAffiliation {搞定;组; }    [JsonProperty(属性名=隶属关系)]
    公共字符串[] {所属获得;组; }    [JsonProperty(属性名=affiliationstatus)]
    公共字符串AffiliationStatus {搞定;组; }    [JsonProperty(属性名=affiliationmodifytimestamp)]
    公众的DateTime AffiliationLastModified {搞定;组; }    [JsonProperty(属性名=雇员)]
    公共字符串雇员{搞定;组; }    [JsonProperty(属性名=accountstatus)]
    公共字符串AccountStatus {搞定;组; }    [JsonProperty(属性名=accountstatusexpiration)]
    公众的DateTime AccountStatusExpiration {搞定;组; }    [JsonProperty(属性名=accountstatusexpmaxdate)]
    公众的DateTime AccountStatusExpirationMaxDate {搞定;组; }    [JsonProperty(属性名=accountstatusmodifytimestamp)]
    公众的DateTime AccountStatusModified {搞定;组; }    [JsonProperty(属性名=accountstatusexpnotice)]
    公共字符串AccountStatusExpNotice {搞定;组; }    [JsonProperty(属性名=accountstatusmodifiedby)]
    公共字典<日期时间,字符串> AccountStatusModifiedBy {搞定;组; }    [JsonProperty(属性名=entrycreatedate)]
    公众的DateTime EntryCreatedate {搞定;组; }    [JsonProperty(属性名=entrydeactivati​​ondate)]
    公众的DateTime EntryDeactivati​​onDate {搞定;组; }  }

和JSON的样本解析是:

  {
    给名字: [
        罗伯特
    ]
    passwordexpired:20091031041550Z,
    accountstatus:[
        活性
    ]
    accountstatusexpiration:[
        20100612000000Z
    ]
    accountstatusexpmaxdate:[
        20110410000000Z
    ]
    accountstatusmodifiedby:{
        20100214173242Z:tdecker,
        20100304003242Z:jsmith的,
        20100324103242Z:jsmith的,
        20100325000005Z:rjones,
        20100326210634Z:jsmith的,
        20100326211130Z:JSMITH
    },
    accountstatusmodifytimestamp:[
        20100312001213Z
    ]
    从属关系:
        雇员,
        承包商,
        员工
    ]
    affiliationmodifytimestamp:[
        20100312001213Z
    ]
    affiliationstatus:[
        分离
    ]
    entrycreatedate:[
        20000922072747Z
    ]
    用户名: [
        rjohnson
    ]
    primaryaffiliation:[
        员工
    ]
    员工ID: [
        999777666
    ]
    SN:[
        约翰逊
    ]
}


解决方案

使用 JsonConvert.DeserializeObject< RootObject>(JSON字符串);

JSON 2 C#

创建类

Json.NET文档: 序列化和反序列化JSON与Json.NET

I'm relatively new to working with C# and JSON data and am seeking guidance. I'm using C# 3.0, with .NET3.5SP1, and JSON.NET 3.5r6.

I have a defined C# class that I need to populate from a JSON structure. However, not every JSON structure for an entry that is retrieved from the web service contains all possible attributes that are defined within the C# class.

I've been being doing what seems to be the wrong, hard way and just picking out each value one by one from the JObject and transforming the string into the desired class property.

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

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

What is the best way to deserialize a JSON structure into the C# class and handling possible missing data from the JSON source?

My class is defined as:

  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; }

  }

And a sample of the JSON to parse is:

{
    "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"
    ]
}

解决方案

Use JsonConvert.DeserializeObject<RootObject>(string json);

Create your classes on JSON 2 C#


Json.NET documentation: Serializing and Deserializing JSON with Json.NET

这篇关于使用JSON数据反序列化到C#JSON.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆