使用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谢谢,我不知道我在搜索现有问题时没有发现。我想我做了一个重复。 –