2012-06-13 60 views
0

我反序列化雅虎聯繫人api的json響應一切都很順利,但我得到的問題在一個字段fields,它的一個數組但在所有元素value是不同的不同,如何處理它。在兩個字段中,這是字符串和其他元素對象。這裏是我的樣品JSONYahoo聯繫API Json deserilization問題

"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": "[email protected]", 
     "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; } 
    } 

回答

0

value屬性是另一個對象。 創建一個名爲ValueField新類,並添加一些屬性:

  1. 給定名稱
  2. 中間名
  3. FamilyName
  4. 前綴
  5. 後綴
  6. GivenNameSound
  7. FamilyNameSound

這個類的屬性添加到您的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() } 
); 
+0

但前兩個元素值是字符串類型 –

+0

我明白了,你可以寫你自己的ContractResolver來檢測,如果Value屬性是否爲String類型。關於這方面的更多信息可以在這裏找到:http://james.newtonking.com/projects/json/help/ContractResolver.html –