【发布时间】:2018-04-05 21:57:44
【问题描述】:
我有一个似乎无法解决的问题。
我有以下课程
public class Foo:IFoo
{
Private List<IBar> _listTest=new List<IBar>();
Public List<IBar> ListTest
{
get
{
return _listTest;
}
set
{
_listTest=value;
}
}
}
当我从 mvc 应用程序发出 webapi 调用并尝试对其进行反序列化时,我收到以下错误
JsonSerializationException:无法创建 IBar 类型的实例。 类型是接口或抽象类,不能实例化。
我知道这可以通过将接口中列表的返回类型更改为 List 来解决,但这似乎不是一个好主意。
反序列化的最佳方法是什么?
【问题讨论】:
-
你看过JSON.NET - how to deserialize collection of interface-instances?或Deserializing polymorphic json classes without type information using json.net或Using Json.NET converters to deserialize properties或How to implement custom JsonConverter in JSON.NET to deserialize a List of base class objects?或how to deserialize JSON into IEnumerable<BaseType> with Newtonsoft JSON.NET吗?
标签: .net asp.net-web-api json.net