c# - 使用 JSON.NET 将 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 源中可能丢失的数据的最佳方法是什么?

我的类(class)定义为:

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

最佳答案

使用

var rootObject =  JsonConvert.DeserializeObject<RootObject>(string json);

在 JSON 2 C# 上创建您的类(class)


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

https://stackoverflow.com/questions/2546138/

相关文章:

json - 在 ASP.NET 中使用 WebAPI 或 MVC 返回 JSON

json - 将 Go 结构转换为 JSON

javascript - 将字符串转换为 JSON 对象

javascript - JSON 劫持在现代浏览器中仍然是一个问题吗?

javascript - 使用 JavaScript 遍历 JSON 对象树的所有节点

ruby-on-rails - 如何使用 RSpec 检查 JSON 响应?

android - 在 Android 中发送和解析 JSON 对象

json - 如何在 Github Wiki 中设置 JSON block 的样式?

json - 将 CSV/XLS 转换为 JSON?

javascript - 序列化包含循环对象值的对象