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; }
}
但前兩個元素值是字符串類型 –
我明白了,你可以寫你自己的ContractResolver來檢測,如果Value屬性是否爲String類型。關於這方面的更多信息可以在這裏找到:http://james.newtonking.com/projects/json/help/ContractResolver.html –