2013-01-03 32 views
4

我有反序列化的問題/序列化與Newtonsoft JSON.NET庫:JSON.NET反序列化/序列化JsonProperty的JSONObject

我收到此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" : "[email protected]", 
     "id" : 50, 
     "fullName" : "name", 
     "department" : "department" 
    } ], 
    "attribute" : { 
     "nbrOfUse" : 1, 
     "lastUse" : "2013-01-03T09:57:28-0500" 
    }  
} 

,我的問題是這部分:

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

在我address類,是有可能做這樣的事情:

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

我不會用JObject.Parse(...)因爲JSON是非常大的

我的類:

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

回答

0

您可以使用的一個可能的解決方案,可能看起來有點亂,可能是在解析它之前操縱JSON字符串,例如使用正則表達式。

你會找到並更換

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

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

然後,你的串行將確定一個DateTime?屬性名稱爲「lastUsed」

1

您需要進行自定義類型 「屬性」,如:

public class Attribute { 

     [JsonProperty(PropertyName = "nbrOfUse")] 
     public int _nbrOfUse { get; set; } 
     [JsonProperty(PropertyName = "streetType")] 
     [Display(Name = "lastUse", ResourceType = typeof(AccountModels))] 
     public string _lastUse { get; set; } 

    } 

並在Address類中創建一個Attribute類的對象。

+0

我不希望在我的地址類中有一個對象屬性...我不能因爲這個類已經用於其他人的視圖..而且它很醜...不是嗎? –