是否可以在不同的名称中反序列化和序列化json对象?

时间:2013-08-23 15:26:31

标签: c# json json.net

让我们说我有这个课程:

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类时,我希望将某些属性(如“地址”)序列化为“地址”而不是“地址”。

我尝试过使用DataMember但没有用。有什么想法吗?

1 个答案:

答案 0 :(得分:3)

[JsonProperty(propertyName: "address")]

将其序列化为"address"

删除该属性,它将序列化为C#Property name。

string json = Newtonsoft.Json.JsonConvert.SerializeObject(Class);