我試圖以反序列化JSON對象到動態列表,所以我有:JSON.NET DeserializeObject從陣列通過以列表
JSON:
string json = "{'elements':[
{'EntityA':[
{'name ':'Jhon'}
],
'EntityB':[
{'title' : 'car'}
],
'EntityB':[
{'title':'aaa'}
],
'EntityA':[
{'name' : 'Alice'}
]]}";
.NET類的基類:
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()
{
}
}
我DeserializeObject難治:
Entitys listFinaly = JsonConvert.DeserializeObject<Entitys>(json);
異常「類型是一個接口或抽象類,不能實例化。 :(「
也許,我會試圖幫助,如果'JsonConvert'不會拋出異常,而解析你的json字符串 – I4V