假设我有这个类:
public class Resource
{
[JsonProperty(propertyName: "address")]
public Domain.DTO.Address Addresses { get; set; }
}
Json字符串值看起来像:
{
.
.
.
"resourceSets":[
{
"estimatedTotal":1,
"resources":[
{
.
.
.
"address":{
"UserLocation":null,
"adminDistrict":"National Capital Region",
"adminDistrict2":"Third District NCR",
"locality":"Caloocan City",
"postalCode":null,
"addressLine":"Yellow Bell Ext",
"formattedAddress":"Yellow Bell Ext, Caloocan City, Philippines",
"neighborhood":"Barangay 161",
"landmark":null,
"countryRegion":"Philippines"
}
}
]
}
]
}
这将正确地反序列化。但是,在序列化Resource类时,我希望像"Addresses"这样的一些属性被序列化为"address",而不是"address"。
我尝试过使用DataMember,但没有成功。知道吗?
[JsonProperty(propertyName: "address")]
使其串行化为CCD_ 1。
删除该属性,它将串行化为C#属性名称。
string json = Newtonsoft.Json.JsonConvert.SerializeObject(Class);