JSON.NET désérialiser / sérialiser JsonProperty JsonObject

J’ai une question pour désérialiser / sérialiser avec la bibliothèque Newtonsoft JSON.NET:

Je reçois ce 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" } ], "atsortingbute" : { "nbrOfUse" : 1, "lastUse" : "2013-01-03T09:57:28-0500" } } 

et mon problème est avec cette partie:

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

Dans ma classe d’ address , est-il possible de faire quelque chose comme:

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

Je n’utiliserais pas JObject.Parse(...) car le json est très gros

Ma classe :

 public class Address { ///  /// Gets or sets the id of the  class. ///  ///  /// id from index as400 for validating address with canada post /// Type Int64, The id. ///  [JsonProperty(PropertyName = "id")] public int Id { get; set; } ///  /// Gets or sets the number of the  class. ///  ///  /// Type Ssortingng, The number. ///  [JsonProperty(PropertyName = "streetNumber")] [Display(Name = "CompanyNumber", ResourceType = typeof(AccountModels))] //[MustBeEmptyIfAnotherIsNotEmpty("PoBox", ErrorMessageResourceType = typeof(GenericValidator), ErrorMessageResourceName = "MustBeEmptyIfAnotherIsNotEmpty")] public ssortingng Number { get; set; } ///  /// Gets or sets the type of the street. ///  ///  /// The type of the street. ///  [JsonProperty(PropertyName = "streetType")] [Display(Name = "StreetType", ResourceType = typeof(AccountModels))] //[MustBeEmptyIfAnotherIsNotEmpty("PoBox",false, ErrorMessageResourceType = typeof(GenericValidator), ErrorMessageResourceName = "MustBeEmptyIfAnotherIsNotEmpty")] public ssortingng StreetType { get; set; } ///  /// Gets or sets the street of the  class. ///  ///  /// Type Ssortingng, The street. ///  [JsonProperty(PropertyName = "streetName")] [Display(Name = "CompanyStreet", ResourceType = typeof(AccountModels))] [SsortingngLength(50, ErrorMessageResourceType = typeof(GenericValidator), ErrorMessageResourceName = "Length", MinimumLength = 2)] //[MustBeEmptyIfAnotherIsNotEmpty("PoBox", ErrorMessageResourceType = typeof(GenericValidator), ErrorMessageResourceName = "MustBeEmptyIfAnotherIsNotEmpty")] public ssortingng Street { get; set; } [Display(Name = "StreetDirection", ResourceType = typeof(AccountModels))] [SsortingngLength(2, ErrorMessageResourceType = typeof(GenericValidator), ErrorMessageResourceName = "Length", MinimumLength = 0)] public ssortingng StreetDirection { get; set; } [Display(Name = "StreetSuffix", ResourceType = typeof(AccountModels))] [SsortingngLength(1, ErrorMessageResourceType = typeof(GenericValidator), ErrorMessageResourceName = "Length", MinimumLength = 0)] public ssortingng StreetSuffix { get; set; } ///  /// Gets or sets the street suite of the  class. ///  ///  /// Type Ssortingng, The street suite. ///  [JsonProperty(PropertyName = "suite")] [Display(Name = "CompanyStreetSuite", ResourceType = typeof(AccountModels))] [SsortingngLength(50, ErrorMessageResourceType = typeof(GenericValidator), ErrorMessageResourceName = "Length", MinimumLength = 0)] //[MustBeEmptyIfAnotherIsNotEmpty("PoBox",false, ErrorMessageResourceType = typeof(GenericValidator), ErrorMessageResourceName = "MustBeEmptyIfAnotherIsNotEmpty")] public ssortingng StreetSuite { get; set; } ///  /// Gets or sets the city of the  class. ///  ///  /// Type Ssortingng, The city. ///  [Required] [JsonProperty(PropertyName = "city")] [Display(Name = "CompanyCity", ResourceType = typeof(AccountModels))] public ssortingng City { get; set; } ///  /// Gets or sets the province of the  class. ///  ///  /// Type Ssortingng, The province. ///  [Required] [Display(Name = "CompanyProvince", ResourceType = typeof(AccountModels))] [JsonProperty(PropertyName = "provinceCode")] public ssortingng Province { get; set; } ///  /// Gets or sets the postal code of the  class. ///  ///  /// Type Ssortingng, The postal code. ///  [JsonProperty(PropertyName = "postalCode")] [Display(Name = "CompanyPostalCode", ResourceType = typeof(AccountModels))] [PostalCode("Country", ErrorMessageResourceType = typeof(GenericValidator), ErrorMessageResourceName = "PostalCode")] public ssortingng PostalCode { get; set; } ///  /// Gets or sets the country of the  class. ///  ///  /// Type Ssortingng, The country. ///  [JsonProperty(PropertyName = "country")] [Display(Name = "Country", ResourceType = typeof(AccountModels))] public ssortingng 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; } } 

Une solution possible que vous pourriez utiliser, ce qui pourrait paraître un peu compliqué, consisterait à manipuler la chaîne JSON avant de l’parsingr, à l’aide d’une expression rationnelle, par exemple.

Vous identifieriez et remplaceriez

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

avec

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

Ensuite, votre sérialiseur identifierait un DateTime? propriété nommée “lastUsed”

Vous devez créer un type personnalisé pour “atsortingbut” comme:

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

Et créez un object de la classe Atsortingbute dans votre classe Address.