Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.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
Javascript 不同的Json序列化和反序列化名称?_Javascript_C#_Serialization_Attributes - Fatal编程技术网

Javascript 不同的Json序列化和反序列化名称?

Javascript 不同的Json序列化和反序列化名称?,javascript,c#,serialization,attributes,Javascript,C#,Serialization,Attributes,是否有一个属性来设置Json序列化名称,然后设置一个不同的反序列化名称。我有一个名为Customer.Age的JObject,我希望它反序列化为Age属性,如果再次序列化,则保持Age。有人知道这是否可以通过一个或两个简单的属性实现吗?JsonProperty确实有效,我只是需要稍微改变一下类的设置。我创建了一个带有参数的构造函数,这些参数上有JsonProperty,然后在构造函数的主体中,我将属性名设置为等于参数名 public class Customer { public Cus

是否有一个属性来设置Json序列化名称,然后设置一个不同的反序列化名称。我有一个名为Customer.Age的JObject,我希望它反序列化为Age属性,如果再次序列化,则保持Age。有人知道这是否可以通过一个或两个简单的属性实现吗?

JsonProperty确实有效,我只是需要稍微改变一下类的设置。我创建了一个带有参数的构造函数,这些参数上有JsonProperty,然后在构造函数的主体中,我将属性名设置为等于参数名

public class Customer
{
    public Customer(
        [JsonProperty("Customer.Name")] string name,
        [JsonProperty("Customer.Email")] string email,
        [JsonProperty("Customer.Country")] string country,
        [JsonProperty("Customer.State")] string state,
        [JsonProperty("Customer.City")] string city,
        [JsonProperty("Customer.Phone")] string phone,
        [JsonProperty("Customer.Company")] string company,
        [JsonProperty("Customer.Region")] string region,
        [JsonProperty("Customer.Fax")] string fax,
        [JsonProperty("Customer.Age")] string age,
        [JsonProperty("Customer.Territory")] string territory,
        [JsonProperty("Customer.Occupation")] string occupation
        )
    {
        Name = name;
        Email = email;
        Country = country;
        State = state;
        City = city;
        PhoneNumber = phone;
        Company = company;
        Region = region;
        FaxNumber = FaxNumber;
        Age = age;
        Territory = territory;
        Occupation = occupation;
    }

    public string Name { get; set; }
    public string Email { get; set; }   
    public string Country { get; set; } 
    public string State { get; set; }
    public string City { get; set; }
    public string PhoneNumber { get; set; }
    public string Company { get; set; }
    public string Region { get; set; }
    public string FaxNumber { get; set; }       
    public string Age { get; set; } 
    public string Territory { get; set; }   
    public string Occupation { get; set; }

}
这使我可以在JObject客户上使用customer.ToObject。

JsonProperty属性有一个重载构造函数,该构造函数接受序列化过程中使用的名称,而不是属性名称本身。