3 回答
TA贡献1812条经验 获得超5个赞
你需要:
JsonSerializerSettings settings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
};
string strJson = JsonConvert.SerializeObject(instance, settings);
所以JSON看起来像这样:
{
"$type": "System.Collections.Generic.List`1[[MyAssembly.BaseClass, MyAssembly]], mscorlib",
"$values": [
{
"$id": "1",
"$type": "MyAssembly.ClassA, MyAssembly",
"Email": "me@here.com",
},
{
"$id": "2",
"$type": "MyAssembly.ClassB, MyAssembly",
"Email": "me@here.com",
}
]
}
然后您可以反序列化它:
BaseClass obj = JsonConvert.DeserializeObject<BaseClass>(strJson, settings)
TA贡献2051条经验 获得超10个赞
反序列化时使用以下JsonSerializerSettings构造:
new JsonSerializerSettings()
{
TypeNameHandling = TypeNameHandling.Objects
})
- 3 回答
- 0 关注
- 687 浏览
添加回答
举报