C# JSON.NET按顺序将对象从数组反序列化到列表
我试图按顺序将Json对象反序列化为动态列表,因此: Json: .NET类基类:C# JSON.NET按顺序将对象从数组反序列化到列表,c#,json,json.net,C#,Json,Json.net,我试图按顺序将Json对象反序列化为动态列表,因此: Json: .NET类基类: public interface EntitysInterface{} public class EntityA: EntitysInterface { public string name { get; set; } } public class EntityB: EntitysInterface { public string title { get; set; } } public cla
public interface EntitysInterface{}
public class EntityA: EntitysInterface
{
public string name { get; set; }
}
public class EntityB: EntitysInterface
{
public string title { get; set; }
}
public class Entitys
{
public List<EntitysInterface> elements { get; set; } //EntityA, EntityB,...
public Entitys()
{
}
}
public interface EntitysInterface{}
公共类EntityA:EntitysInterface
{
公共字符串名称{get;set;}
}
公共类EntityB:EntitysInterface
{
公共字符串标题{get;set;}
}
公共类实体
{
公共列表元素{get;set;}//EntityA,EntityB,。。。
公共实体()
{
}
}
我的反序列化对象问题:
Entitys listFinaly = JsonConvert.DeserializeObject<Entitys>(json);
Entitys listFinaly=JsonConvert.DeserializeObject(json);
Exception”类型是一个接口或抽象类,无法实例化。(“JSON.NET反序列化程序告诉您,它无法确定您希望选择的接口对象是哪个
实体。它需要您给它一个提示
ContractResolver
有助于给出提示
编辑:
在JSON.Net文档中有一个错误。如果JsonConvert
在解析JSON字符串时不会引发异常,我可能会尝试提供帮助
Entitys listFinaly = JsonConvert.DeserializeObject<Entitys>(json);