Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Yahoo联系人API Json反序列化问题_C#_.net_Json_Json.net_Yahoo Api - Fatal编程技术网

C# Yahoo联系人API Json反序列化问题

C# Yahoo联系人API Json反序列化问题,c#,.net,json,json.net,yahoo-api,C#,.net,Json,Json.net,Yahoo Api,我正在反序列化yahoo contacts api的json响应一切都很顺利,但我在一个字段中遇到了问题字段,它是一个数组,但在所有元素中值不同,如何处理它。在两个字段中,这是字符串,在另一个元素对象中。这是我的json示例 "fields": [ { "created": "2008-12-29T13:47:21Z", "updated": "2008-12-29T13:47:21Z", "uri": "http://social.yaho

我正在反序列化yahoo contacts api的json响应一切都很顺利,但我在一个字段中遇到了问题
字段
,它是一个数组,但在所有元素中
不同,如何处理它。在两个字段中,这是字符串,在另一个元素对象中。这是我的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"
      }
     ]
我为字段创建了以下类

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; }
    }

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

  • 吉文纳姆
  • 中间名
  • 家庭名称
  • 前缀
  • 后缀
  • 吉文纳中音
  • 家庭美声
  • 并将该类作为属性添加到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; }
        }
    
    您应该编写自己的契约解析器来检测属性值是否为String类型。通过这种方式,您可以中断默认行为并实现自己的逻辑

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

    public class CustomContractResolver: DefaultContractResolver
    {
    //override the methods you need.
    }
    
    您可以通过执行以下操作来设置自定义合同解析程序:

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

    但在前两个元素中,Value是string类型,我看到了,您可以编写自己的ContractResolver来检测Value属性是否为string类型。有关这方面的更多信息,请参见: