JSON.NET反序列化/序列化JsonProperty的JSONObject [英] JSON.NET deserialize/serialize JsonProperty JsonObject

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

问题描述

我有反序列化/问题与Newtonsoft JSON.NET库序列化:

我收到此JSON:

  {
    客户名称:排版
    ID:492426
    序:1232132
    类型:3,
    状态:0,
    streetNumber:9675,
    streetName:排版
    套房:空,
    城市:IPSUM
    省code:QC
    邮政code:H1P1Z3
    routeNumber:0,
    信箱:0,
    streetType:CH,
    用户id:25,
    streetDirection:空,
    国家code:CA,
    customerNickName:Lorem存有
    streetSuffix:空,
    人脉:[{
        状态:0,
        电话:4445555555,
        延伸:0,
        电子邮件:webtest@test.com
        ID:50,
        全名:名字,
        部:部
    }],
    属性:{
      nbrOfUse:1,
      lastUse:2013-01-03T09:57:28-0500
    }
}

和我的问题是这样的部分:

 属性:{
      nbrOfUse:1,
      lastUse:2013-01-03T09:57:28-0500
    }

在我的地址类,是有可能做这样的事情:

  [JsonProperty(属性名=lastUse的ObjectName =属性)]
    或[JsonProperty(属性名=lastUse)] [JSONObject的(属性名=属性)]
    或者这样的事情...
    公众的DateTime? LastUse {搞定;组; }

我不会用 JObject.Parse(...)由于JSON是非常大的。

我的类:

 公共类地址
{
    ///<总结>
    ///获取或设置在&lt标识;见CREF =地址/>类。
    ///< /总结>
    ///< VALUE>
    ///从指数AS400 ID为与加拿大邮政地址验证
    ///类型的Int64,身份证。
    ///< /值>
    [JsonProperty(属性名=ID)]
    公众诠释标识{搞定;组; }    ///<总结>
    ///获取或设置在&lt数;见CREF =公司/>类。
    ///< /总结>
    ///< VALUE>
    ///类型字符串,数量。
    ///< /值>
    [JsonProperty(属性名=streetNumber)]
    [显示(NAME =CompanyNumber的ResourceType = typeof运算(AccountModels))]
    // [MustBeEmptyIfAnotherIsNotEmpty(信箱,ErrorMessageResourceType = typeof运算(GenericValidator),ErrorMessageResourceName =MustBeEmptyIfAnotherIsNotEmpty)]
    公共弦数{搞定;组; }    ///<总结>
    ///获取或设置街道的类型。
    ///< /总结>
    ///< VALUE>
    ///类型的街道。
    ///< /值>
    [JsonProperty(属性名=streetType)]
    [显示(NAME =StreetType的ResourceType = typeof运算(AccountModels))]
    // [MustBeEmptyIfAnotherIsNotEmpty(信箱,假的,ErrorMessageResourceType = typeof运算(GenericValidator),ErrorMessageResourceName =MustBeEmptyIfAnotherIsNotEmpty)]
    公共字符串StreetType {搞定;组; }    ///<总结>
    ///获取或设置在&lt街道;见CREF =公司/>类。
    ///< /总结>
    ///< VALUE>
    ///类型字符串,这条街。
    ///< /值>
    [JsonProperty(属性名=streetName)]
    [显示(NAME =CompanyStreet的ResourceType = typeof运算(AccountModels))]
    [StringLength(50,ErrorMessageResourceType = typeof运算(GenericValidator),ErrorMessageResourceName =长度,MinimumLength = 2)]
    // [MustBeEmptyIfAnotherIsNotEmpty(信箱,ErrorMessageResourceType = typeof运算(GenericValidator),ErrorMessageResourceName =MustBeEmptyIfAnotherIsNotEmpty)]
    公共字符串街{搞定;组; }    [显示(NAME =StreetDirection的ResourceType = typeof运算(AccountModels))]
    [StringLength(2,ErrorMessageResourceType = typeof运算(GenericValidator),ErrorMessageResourceName =长度,MinimumLength = 0)]
    公共字符串StreetDirection {搞定;组; }    [显示(NAME =StreetSuffix的ResourceType = typeof运算(AccountModels))]
    [StringLength(1,ErrorMessageResourceType = typeof运算(GenericValidator),ErrorMessageResourceName =长度,MinimumLength = 0)]
    公共字符串StreetSuffix {搞定;组; }    ///<总结>
    ///获取或设置在&lt大街套房;见CREF =公司/>类。
    ///< /总结>
    ///< VALUE>
    ///类型字符串,这条街的套房。
    ///< /值>
    [JsonProperty(属性名=套件)]
    [显示(NAME =CompanyStreetSuite的ResourceType = typeof运算(AccountModels))]
    [StringLength(50,ErrorMessageResourceType = typeof运算(GenericValidator),ErrorMessageResourceName =长度,MinimumLength = 0)]
    // [MustBeEmptyIfAnotherIsNotEmpty(信箱,假的,ErrorMessageResourceType = typeof运算(GenericValidator),ErrorMessageResourceName =MustBeEmptyIfAnotherIsNotEmpty)]
    公共字符串StreetSuite {搞定;组; }    ///<总结>
    ///获取或设置城市的<见CREF =公司/>类。
    ///< /总结>
    ///< VALUE>
    ///类型字符串,该城市。
    ///< /值>
    [需要]
    [JsonProperty(属性名=城市)]
    [显示(NAME =CompanyCity的ResourceType = typeof运算(AccountModels))]
    公共字符串城{搞定;组; }    ///<总结>
    ///获取或设置在&lt省;见CREF =公司/>类。
    ///< /总结>
    ///< VALUE>
    ///类型字符串,全省。
    ///< /值>
    [需要]
    [显示(NAME =CompanyProvince的ResourceType = typeof运算(AccountModels))]
    [JsonProperty(属性名=省code)]
    公共字符串省{搞定;组; }    ///<总结>
    ///获取或设置在&lt邮政code;见CREF =公司/>类。
    ///< /总结>
    ///< VALUE>
    ///类型字符串,邮政code。
    ///< /值>
    [JsonProperty(属性名=邮政code)]
    [显示(NAME =CompanyPostal code的ResourceType = typeof运算(AccountModels))]
    [邮政code(国家,ErrorMessageResourceType = typeof运算(GenericValidator),ErrorMessageResourceName =邮政code)]
    公共字符串邮政code {搞定;组; }    ///<总结>
    ///获取或设置在&lt国;见CREF =公司/>类。
    ///< /总结>
    ///< VALUE>
    ///类型字符串,该国。
    ///< /值>
    [JsonProperty(属性名=国)]
    [显示(名称=国家的ResourceType = typeof运算(AccountModels))]
    公共字符串国家{搞定;组; }    // // ....    / *记录信息* /
    [显示(NAME =modifyDate的ResourceType = typeof运算(Resources.Models.Address))]
    公众的DateTime ModifyDate {搞定;组; }    [显示(NAME =结束日期的ResourceType = typeof运算(Resources.Models.Address))]
    公众的DateTime?结束日期{搞定;组; } //当她停用    [显示(NAME =lastUse的ResourceType = typeof运算(Resources.Models.Address))]
    公众的DateTime? LastUse {搞定;组; }    [显示(NAME =nbrOfUse的ResourceType = typeof运算(Resources.Models.Address))]
    公众诠释NbrOfUse {搞定;组; }
}


解决方案

一个可能的解决方案,您可以使用,这可能看起来有点哈克,将解析它,例如使用正则表达式之前,操纵JSON字符串。

您会找到并更换

 属性:{
   nbrOfUse:1,
   lastUse:2013-01-03T09:57:28-0500
 }

 lastUse:2013-01-03T09:57:28-0500

然后,您的序列化将确定一个DateTime?命名属性LASTUSED

I have a question for deserialize/serialize with the Newtonsoft JSON.NET library:

I receive this json :

 {
    "customerName" : "Lorem",
    "id": 492426
    "sequence": 1232132
    "type" : 3,
    "status" : 0,
    "streetNumber" : 9675,
    "streetName" : "Lorem",
    "suite" : null,
    "city" : "IPSUM",
    "provinceCode" : "QC",
    "postalCode" : "H1P1Z3",
    "routeNumber" : 0,
    "poBox" : 0,
    "streetType" : "CH",
    "userId" : 25,
    "streetDirection" : null,
    "countryCode" : "CA",
    "customerNickName" : "Lorem ipsum",
    "streetSuffix" : null,
    "contacts" : [ {
        "status" : 0,
        "telephone" : 4445555555,
        "extension" : 0,
        "email" : "webtest@test.com",
        "id" : 50,
        "fullName" : "name",
        "department" : "department"
    } ],
    "attribute" : {
      "nbrOfUse" : 1,
      "lastUse" : "2013-01-03T09:57:28-0500"
    }       
}

and my problem is with this part :

    "attribute" : {
      "nbrOfUse" : 1,
      "lastUse" : "2013-01-03T09:57:28-0500"
    }

In my address class, is it possible to do something like:

    [JsonProperty(PropertyName = "lastUse", ObjectName="attribute")]
    or [JsonProperty(PropertyName = "lastUse")][JsonObject(PropertyName = "attribute")]
    or something like this ...
    public DateTime? LastUse { get; set; }

I wouldn't use JObject.Parse(...) because the json is very big

My class :

public class Address
{
    /// <summary>
    /// Gets or sets the id of the <see cref="Address" /> class.
    /// </summary>
    /// <value>
    /// id from index as400 for validating address with canada post
    /// Type Int64, The id.
    /// </value>
    [JsonProperty(PropertyName = "id")]
    public int Id { get; set; }

    /// <summary>
    /// Gets or sets the number of the <see cref="Company" /> class.
    /// </summary>
    /// <value>
    /// Type String, The number.
    /// </value>
    [JsonProperty(PropertyName = "streetNumber")]
    [Display(Name = "CompanyNumber", ResourceType = typeof(AccountModels))]
    //[MustBeEmptyIfAnotherIsNotEmpty("PoBox", ErrorMessageResourceType = typeof(GenericValidator), ErrorMessageResourceName = "MustBeEmptyIfAnotherIsNotEmpty")]
    public string Number { get; set; }

    /// <summary>
    /// Gets or sets the type of the street.
    /// </summary>
    /// <value>
    /// The type of the street.
    /// </value>
    [JsonProperty(PropertyName = "streetType")]
    [Display(Name = "StreetType", ResourceType = typeof(AccountModels))]
    //[MustBeEmptyIfAnotherIsNotEmpty("PoBox",false, ErrorMessageResourceType = typeof(GenericValidator), ErrorMessageResourceName = "MustBeEmptyIfAnotherIsNotEmpty")]
    public string StreetType { get; set; }

    /// <summary>
    /// Gets or sets the street of the <see cref="Company" /> class.
    /// </summary>
    /// <value>
    /// Type String, The street.
    /// </value>
    [JsonProperty(PropertyName = "streetName")]
    [Display(Name = "CompanyStreet", ResourceType = typeof(AccountModels))]
    [StringLength(50, ErrorMessageResourceType = typeof(GenericValidator), ErrorMessageResourceName = "Length", MinimumLength = 2)]
    //[MustBeEmptyIfAnotherIsNotEmpty("PoBox", ErrorMessageResourceType = typeof(GenericValidator), ErrorMessageResourceName = "MustBeEmptyIfAnotherIsNotEmpty")]
    public string Street { get; set; }

    [Display(Name = "StreetDirection", ResourceType = typeof(AccountModels))]
    [StringLength(2, ErrorMessageResourceType = typeof(GenericValidator), ErrorMessageResourceName = "Length", MinimumLength = 0)]
    public string StreetDirection { get; set; }

    [Display(Name = "StreetSuffix", ResourceType = typeof(AccountModels))]
    [StringLength(1, ErrorMessageResourceType = typeof(GenericValidator), ErrorMessageResourceName = "Length", MinimumLength = 0)]
    public string StreetSuffix { get; set; }

    /// <summary>
    /// Gets or sets the street suite of the <see cref="Company" /> class.
    /// </summary>
    /// <value>
    /// Type String, The street suite.
    /// </value>
    [JsonProperty(PropertyName = "suite")]
    [Display(Name = "CompanyStreetSuite", ResourceType = typeof(AccountModels))]
    [StringLength(50, ErrorMessageResourceType = typeof(GenericValidator), ErrorMessageResourceName = "Length", MinimumLength = 0)]
    //[MustBeEmptyIfAnotherIsNotEmpty("PoBox",false, ErrorMessageResourceType = typeof(GenericValidator), ErrorMessageResourceName = "MustBeEmptyIfAnotherIsNotEmpty")]
    public string StreetSuite { get; set; }

    /// <summary>
    /// Gets or sets the city of the <see cref="Company" /> class.
    /// </summary>
    /// <value>
    /// Type String, The city.
    /// </value>
    [Required]
    [JsonProperty(PropertyName = "city")]
    [Display(Name = "CompanyCity", ResourceType = typeof(AccountModels))]
    public string City { get; set; }

    /// <summary>
    /// Gets or sets the province of the <see cref="Company" /> class.
    /// </summary>
    /// <value>
    /// Type String, The province.
    /// </value>
    [Required]
    [Display(Name = "CompanyProvince", ResourceType = typeof(AccountModels))]
    [JsonProperty(PropertyName = "provinceCode")]
    public string Province { get; set; }

    /// <summary>
    /// Gets or sets the postal code of the <see cref="Company" /> class.
    /// </summary>
    /// <value>
    /// Type String, The postal code.
    /// </value>
    [JsonProperty(PropertyName = "postalCode")]
    [Display(Name = "CompanyPostalCode", ResourceType = typeof(AccountModels))]
    [PostalCode("Country", ErrorMessageResourceType = typeof(GenericValidator), ErrorMessageResourceName = "PostalCode")]
    public string PostalCode { get; set; }

    /// <summary>
    /// Gets or sets the country of the <see cref="Company" /> class.
    /// </summary>
    /// <value>
    /// Type String, The country.
    /// </value>
    [JsonProperty(PropertyName = "country")]
    [Display(Name = "Country", ResourceType = typeof(AccountModels))]
    public string Country { get; set; }

    // ....//

    /*record info*/
    [Display(Name = "modifyDate", ResourceType = typeof(Resources.Models.Address))]
    public DateTime ModifyDate { get; set; }

    [Display(Name = "endDate", ResourceType = typeof(Resources.Models.Address))]
    public DateTime? EndDate { get; set; } // when she deactivated

    [Display(Name = "lastUse", ResourceType = typeof(Resources.Models.Address))]
    public DateTime? LastUse { get; set; }

    [Display(Name = "nbrOfUse", ResourceType = typeof(Resources.Models.Address))]
    public int NbrOfUse { get; set; }
}

解决方案

One possible solution you could use, which might look a bit hacky, would be to manipulate the JSON string before parsing it, using a regex for example.

You would identify and replace

"attribute" : {
   "nbrOfUse" : 1,
   "lastUse" : "2013-01-03T09:57:28-0500"
 }

with

"lastUse" : "2013-01-03T09:57:28-0500"

Then, your serializer would identify a DateTime? property named "lastUsed"

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

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