不同的 Json 序列化和反序列化名称?
Different Json serialization and deserialization names?
是否有一个属性来设置一个 Json 序列化名称,然后是一个不同的反序列化名称。我有一个 JObject
,其名称类似于 Customer.Age
,我希望它反序列化为 Age
属性,如果再次序列化则保留 Age
。任何人都知道这是否可以通过一个或两个简单的属性来实现?
JsonProperty
起作用了我只需要稍微不同地设置 class。我用带有 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<Customer>()
。
是否有一个属性来设置一个 Json 序列化名称,然后是一个不同的反序列化名称。我有一个 JObject
,其名称类似于 Customer.Age
,我希望它反序列化为 Age
属性,如果再次序列化则保留 Age
。任何人都知道这是否可以通过一个或两个简单的属性来实现?
JsonProperty
起作用了我只需要稍微不同地设置 class。我用带有 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<Customer>()
。