Yahoo Contact API Json deserilization问题

时间:2012-06-13 06:47:12

标签: c# .net json json.net yahoo-api

我正在反序列化yahoo联系人api的json响应一切顺利但我在一个字段fields中遇到问题,它是一个数组但在所有元素value中有所不同,如何处理它。在两个字段中,这是字符串和其他元素对象。这是我的样本json

"fields": [
      {
       "created": "2008-12-29T13:47:21Z",
       "updated": "2008-12-29T13:47:21Z",
       "uri": "http://social.yahooapis.com/v1/user/ASASASASASASA/contact/8/email/18",
       "id": "18",
       "type": "email",
       "value": "papi@ymail.com",
       "editedBy": "OWNER"
      },
      {
       "created": "2010-03-30T07:02:04Z",
       "updated": "2011-06-25T05:01:51Z",
       "uri": "http://social.yahooapis.com/v1/user/ASASASASASASA/contact/8/guid/42",
       "id": "42",
       "type": "guid",
       "value": "BMM5JTQVDB7G4EBPO2D5ESE3TI",
       "editedBy": "OWNER",
       "isConnection": "false"
      },
      {
       "created": "2008-12-29T13:47:21Z",
       "updated": "2008-12-29T13:47:21Z",
       "uri": "http://social.yahooapis.com/v1/user/ASASASASASASA/contact/8/name/17",
       "id": "17",
       "type": "name",
       "value": {
        "givenName": "Hitesh",
        "middleName": null,
        "familyName": "Lohar",
        "prefix": null,
        "suffix": null,
        "givenNameSound": null,
        "familyNameSound": null
       },
       "editedBy": "OWNER"
      }
     ]

我为Field

创建了以下类
public class YahooField
    {
        [JsonProperty("created")]
        public string Created { get; set; }

        [JsonProperty("updated")]
        public string Updated { get; set; }

        [JsonProperty("uri")]
        public string Uri { get; set; }

        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("type")]
        public string Type { get; set; }

        //here confusion
        //[JsonProperty("value")]
        //public (String or class) Value { get; set; }

        [JsonProperty("editedBy")]
        public string EditedBy { get; set; }

        [JsonProperty("isConnection")]
        public string IsConnection { get; set; }
    }

1 个答案:

答案 0 :(得分:0)

value属性只是另一个对象。 创建一个名为ValueField的新类并添加一些属性:

  1. 给定名称
  2. 中间名
  3. FamilyName
  4. 前缀
  5. 后缀
  6. GivenNameSound
  7. FamilyNameSound
  8. 将此类作为属性添加到YahooField类

    public class YahooField
        {
            [JsonProperty("created")]
            public string Created { get; set; }
    
            [JsonProperty("updated")]
            public string Updated { get; set; }
    
            [JsonProperty("uri")]
            public string Uri { get; set; }
    
            [JsonProperty("id")]
            public string Id { get; set; }
    
            [JsonProperty("type")]
            public string Type { get; set; }
    
            [JsonProperty("value")]
            public ValueField Value { get; set; }
    
            [JsonProperty("editedBy")]
            public string EditedBy { get; set; }
    
            [JsonProperty("isConnection")]
            public string IsConnection { get; set; }
        }
    

    您应编写自己的合约解析程序,以检测属性Value是否为String类型。这样您就可以中断默认行为并实现自己的逻辑。

    只需从DefaultContractResolver派生您的CustomContractResolver,并覆盖实现所需的虚拟方法。

    public class CustomContractResolver: DefaultContractResolver
    {
    //override the methods you need.
    }
    

    您可以通过执行以下操作来设置自定义合约解析程序:

      JsonConvert.SerializeObject(
        product,
        Formatting.Indented,
        new JsonSerializerSettings { ContractResolver = new CustomContractResolver() }
      );