1

假设我有这个类:

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"
              }
           }
        ]
     }
  ]
}  

这会正确反序列化。但是在序列化资源类时,我希望将某些属性(如“地址”)序列化为“地址”而不是“地址”。

我曾尝试使用 DataMember,但没有奏效。任何想法?

4

1 回答 1

3
[JsonProperty(propertyName: "address")]

正在使其序列化为"address".

删除该属性,它将序列化为 C# 属性名称。

string json = Newtonsoft.Json.JsonConvert.SerializeObject(Class);
于 2013-08-23T15:29:13.917 回答