0

我正在反序列化 yahoo Contacts 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; }
    }
4

1 回答 1

0

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

  1. 给定的名称
  2. 中间名字
  3. 字首
  4. 后缀
  5. GivenNameSound
  6. 姓氏音

并将此类作为属性添加到您的 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() }
  );
于 2012-06-13T06:50:58.030 回答