我試圖將一串JSON數據轉換爲一個C#類對象。不過,我在本質上是動態的JSON的一小部分有問題。Json DeserializeObject到具有動態屬性的類
的JSON的部分是以下:
"contact": [{
"comment": null,
"type": {
"id": "cell",
"name": "Example name"
},
"preferred": true,
"value": {
"country": "7",
"formatted": "+7 (702) 344-3423-3",
"number": "3498908",
"city": "702"
}
},
{
"type": {
"id": "email",
"name": "Email example"
},
"preferred": false,
"value": "[email protected]"
}]
C#類
public class Value
{
public string country { get; set; }
public string formatted { get; set; }
public string number { get; set; }
public string city { get; set; }
}
public class Type
{
public string id { get; set; }
public string name { get; set; }
}
public class Contact
{
public string comment { get; set; }
public Type type { get; set; }
public bool preferred { get; set; }
public string value { get; set; }
}
C#代碼
Contact contact = JsonConvert.DeserializeObject<Contact>(result);
的根據接觸信息 「值」 的變化的格式。是否可以將值映射爲字符串和類值?
感謝您提供任何幫助。
對我來說,它看起來像你的類結構錯了。看起來像有一個基類Value和另外兩個類AddressValue和EmailValue。 –