使用C#,反序列化動態JSON對象的正確方法是什麼(即:某個JSON可以在對象之間更改單個鍵的類型)?自定義動態JSON反序列化
例如,如果我有這樣的(完全有效的)JSON:
{
"stuffs": [
{ "content": "abcd" },
{ "content": "efgh" },
{ "content": [
"ijkl",
"mnop"
]}
]
}
其中thing
可以是字符串或字符串數組,如何反序列化這一點,如果我已經定義這些類?
class ThingContainer {
List<Thing> stuffs;
}
class Thing {
List<string> content;
}
試圖反序列化I(果然)碰到一個例外:
Newtonsoft.Json.JsonSerializationException:「不能反序列化JSON當前對象(例如{ 「名」: 「值」 })類型'System.Collections.Generic.List`1 [DummyProject.Thing]'
http://stackoverflow.com/questions/18994685/how-to-handle-both-a-single-item-and-an-array-for-the-same-property-using-json-n此帖子將幫助您設計解決方案。 – Sameer
@Sameer謝謝,我不知道我在搜索現有問題時沒有發現。我想我做了一個重複。 –