Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# JSON.NET反序列化/序列化JsonProperty JsonObject_C#_Asp.net Mvc_Json.net - Fatal编程技术网

C# JSON.NET反序列化/序列化JsonProperty JsonObject

C# JSON.NET反序列化/序列化JsonProperty JsonObject,c#,asp.net-mvc,json.net,C#,Asp.net Mvc,Json.net,我对Newtonsoft JSON.NET库的反序列化/序列化有一个问题: 我收到这个json: { "customerName" : "Lorem", "id": 492426 "sequence": 1232132 "type" : 3, "status" : 0, "streetNumber" : 9675, "streetName" : "Lorem", "suite" : null, "city" : "IPSU

我对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
{
    /// <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; }
}
公共类地址
{
/// 
///获取或设置类的id。
/// 
/// 
///索引as400中用于验证加拿大邮政地址的id
///键入Int64,即id。
/// 
[JsonProperty(PropertyName=“id”)]
公共int Id{get;set;}
/// 
///获取或设置类的编号。
/// 
/// 
///输入字符串,数字。
/// 
[JsonProperty(PropertyName=“streetNumber”)]
[显示(Name=“CompanyNumber”,ResourceType=typeof(AccountModels))]
//[MustBeEmptyIfAnotherIsNotEmpty(“PoBox”,ErrorMessageResourceType=typeof(GenericValidator),ErrorMessageResourceName=“MustBeEmptyIfAnotherIsNotEmpty”)]
公共字符串编号{get;set;}
/// 
///获取或设置街道的类型。
/// 
/// 
///街道的类型。
/// 
[JsonProperty(PropertyName=“streetType”)]
[显示(Name=“StreetType”,ResourceType=typeof(AccountModels))]
//[MustBeEmptyIfAnotherIsNotEmpty(“PoBox”,false,ErrorMessageResourceType=typeof(GenericValidator),ErrorMessageResourceName=“MustBeEmptyIfAnotherIsNotEmpty”)]
公共字符串StreetType{get;set;}
/// 
///获取或设置类的街道。
/// 
/// 
///输入String,街道。
/// 
[JsonProperty(PropertyName=“streetName”)]
[显示(Name=“CompanyStreet”,ResourceType=typeof(AccountModels))]
[StringLength(50,ErrorMessageResourceType=typeof(GenericValidator),ErrorMessageResourceName=“Length”,MinimumLength=2)]
//[MustBeEmptyIfAnotherIsNotEmpty(“PoBox”,ErrorMessageResourceType=typeof(GenericValidator),ErrorMessageResourceName=“MustBeEmptyIfAnotherIsNotEmpty”)]
公共字符串Street{get;set;}
[显示(Name=“StreetDirection”,ResourceType=typeof(AccountModels))]
[StringLength(2,ErrorMessageResourceType=typeof(GenericValidator),ErrorMessageResourceName=“Length”,MinimumLength=0)]
公共字符串StreetDirection{get;set;}
[显示(Name=“StreetSuffix”,ResourceType=typeof(AccountModels))]
[StringLength(1,ErrorMessageResourceType=typeof(GenericValidator),ErrorMessageResourceName=“Length”,MinimumLength=0)]
公共字符串StreetSuffix{get;set;}
/// 
///获取或设置类的街道套件。
/// 
/// 
///输入String,街道套件。
/// 
[JsonProperty(PropertyName=“suite”)]
[显示(Name=“CompanyStreetSuite”,ResourceType=typeof(AccountModels))]
[StringLength(50,ErrorMessageResourceType=typeof(GenericValidator),ErrorMessageResourceName=“Length”,MinimumLength=0)]
//[MustBeEmptyIfAnotherIsNotEmpty(“PoBox”,false,ErrorMessageResourceType=typeof(GenericValidator),ErrorMessageResourceName=“MustBeEmptyIfAnotherIsNotEmpty”)]
公共字符串StreetSuite{get;set;}
/// 
///获取或设置类的城市。
/// 
/// 
///输入字符串,城市。
/// 
[必需]
[JsonProperty(PropertyName=“city”)]
[显示(Name=“CompanyCity”,ResourceType=typeof(AccountModels))]
公共字符串City{get;set;}
/// 
///获取或设置类的省。
/// 
/// 
///输入String,省。
/// 
[必需]
[显示(Name=“CompanyProvince”,ResourceType=typeof(AccountModels))]
[JsonProperty(PropertyName=“provinceCode”)]
公共字符串省{get;set;}
/// 
///获取或设置类的邮政编码。
/// 
/// 
///键入字符串,即邮政编码。
/// 
[JsonProperty(PropertyName=“postalCode”)]
[显示(Name=“CompanyPostalCode”,ResourceType=typeof(AccountModels))]
[PostalCode(“国家”,ErrorMessageResourceType=typeof(GenericValidator),ErrorMessageResourceName=“PostalCode”)]
公共字符串PostalCode{get;set;}
/// 
///获取或设置类的国家/地区。
/// 
/// 
///键入字符串,输入国家/地区。
/// 
[JsonProperty(PropertyName=“country”)]
[显示(Name=“Country”,ResourceType=typeof(AccountModels))]
公共字符串国家{get;set;}
// ....//
/*记录信息*/
[显示(Name=“modifyDate”,ResourceType=typeof(Resources.Models.Address))]
公共日期时间修改日期{get;set;}
[显示(Name=“endDate”,ResourceType=typeof(Resources.Models.Address))]
public DateTime?当她停用时,EndDate{get;set;}//
[显示(Name=“lastUse”,ResourceType=typeof(Resources.Models.Address))]
公共日期时间?LastUse{get;set;}
[显示(Name=“nbrOfUse”,ResourceType=typeof(Resources.Models.Address))]
public int NbrOfUse{get;set;}
}

您需要为“属性”创建自定义类型,如:


并在Address类中创建属性类的对象。

您可以使用的一种可能的解决方案,可能看起来有点老套,就是在解析JSON字符串之前操作它,例如使用正则表达式

您将识别并替换

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


然后,序列化程序将标识日期时间?名为“lastUsed”的属性

我不想在address类中使用对象属性。。。我不能,因为该类已用于其他视图。。而且很难看。。。不
"attribute" : {
   "nbrOfUse" : 1,
   "lastUse" : "2013-01-03T09:57:28-0500"
 }
"lastUse" : "2013-01-03T09:57:28-0500"