你需要:
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": "[email protected] /cdn-cgi/l/email-protection",
},
{
"$id": "2",
"$type": "MyAssembly.ClassB, MyAssembly",
"Email": "[email protected] /cdn-cgi/l/email-protection",
}
]
}
然后你可以反序列化它:
BaseClass obj = JsonConvert.DeserializeObject<BaseClass>(strJson, settings);
文档:类型名称处理设置 http://www.newtonsoft.com/json/help/html/SerializeTypeNameHandling.htm