JSON.NET反序列化/序列化JsonProperty JsonObject
我有一个关于使用Newtonsoft JSON.NET库进行反序列化/序列化的问题:
我收到这个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" } }
我的问题在于这一部分:
"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 { /// /// 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 String, The number. /// [JsonProperty(PropertyName = "streetNumber")] [Display(Name = "CompanyNumber", ResourceType = typeof(AccountModels))] //[MustBeEmptyIfAnotherIsNotEmpty("PoBox", ErrorMessageResourceType = typeof(GenericValidator), ErrorMessageResourceName = "MustBeEmptyIfAnotherIsNotEmpty")] public string 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 string StreetType { get; set; } /// /// Gets or sets the street of the class. /// /// /// Type String, The street. /// [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; } /// /// Gets or sets the street suite of the class. /// /// /// Type String, The street suite. /// [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; } /// /// Gets or sets the city of the class. /// /// /// Type String, The city. /// [Required] [JsonProperty(PropertyName = "city")] [Display(Name = "CompanyCity", ResourceType = typeof(AccountModels))] public string City { get; set; } /// /// Gets or sets the province of the class. /// /// /// Type String, The province. /// [Required] [Display(Name = "CompanyProvince", ResourceType = typeof(AccountModels))] [JsonProperty(PropertyName = "provinceCode")] public string Province { get; set; } /// /// Gets or sets the postal code of the class. /// /// /// Type String, The postal code. /// [JsonProperty(PropertyName = "postalCode")] [Display(Name = "CompanyPostalCode", ResourceType = typeof(AccountModels))] [PostalCode("Country", ErrorMessageResourceType = typeof(GenericValidator), ErrorMessageResourceName = "PostalCode")] public string PostalCode { get; set; } /// /// Gets or sets the country of the class. /// /// /// Type String, The country. /// [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; } }
你可以使用的一个可能的解决方案,可能看起来有点hacky,就是在解析之前操纵JSON字符串,例如使用正则表达式。
你会识别和替换
"attribute" : { "nbrOfUse" : 1, "lastUse" : "2013-01-03T09:57:28-0500" }
同
"lastUse" : "2013-01-03T09:57:28-0500"
然后,您的序列化程序将识别DateTime? 名为“lastUsed”的属性
您需要为“属性”创建自定义类型,如:
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类中创建一个Object of Attribute类。
上述就是C#学习教程:JSON.NET反序列化/序列化JsonProperty JsonObject分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/987875.html