通过 JSON.NET 反序列化另一个 class 中的接口列表
Deserialization by JSON.NET of a list of interfaces inside another class
我已经把其他问题都浏览透了,但我无法解决这个问题。
下面的JSON
[
{
"$type": "UWP.Model.Management.VirtualCarPark.Item.AggregatorItem, UWP.Model",
"UId": 17,
"Name": "K2 BDB-INCON4-U",
"IsPublished": true,
"Items": [{
"$type": "UWP.Model.Management.VirtualCarPark.Item.Item, UWP.Model",
"IsPublished": true,
"UId": 18,
"OwnerUId": 17,
"Name": "1: Root - I/O Modules K2 Switch 1",
"UIdStreet": 1,
"UIdArea": 1
},
{
"$type": "UWP.Model.Management.VirtualCarPark.Item.Item, UWP.Model",
"IsPublished": true,
"UId": 19,
"OwnerUId": 17,
"Name": "2: Root - I/O Modules K2 Switch 2",
"UIdStreet": 1,
"UIdArea": 1
}
]
},
{
"$type": "UWP.Model.Management.VirtualCarPark.Item.AggregatorItem, UWP.Model",
"UId": 41,
"Name": "K3 SH2INDI424",
"IsPublished": true,
"Items": [{
"$type": "UWP.Model.Management.VirtualCarPark.Item.Item, UWP.Model",
"IsPublished": true,
"UId": 42,
"OwnerUId": 41,
"Name": "1: Root - Switches K3 Switch 1",
"UIdStreet": 1,
"UIdArea": 1
},
{
"$type": "UWP.Model.Management.VirtualCarPark.Item.Item, UWP.Model",
"IsPublished": true,
"UId": 43,
"OwnerUId": 41,
"Name": "2: Root - Switches K3 Switch 2",
"UIdStreet": 1,
"UIdArea": 1
},
{
"$type": "UWP.Model.Management.VirtualCarPark.Item.Item, UWP.Model",
"IsPublished": true,
"UId": 44,
"OwnerUId": 41,
"Name": "3: Root - Switches K3 Switch 3",
"UIdStreet": 1,
"UIdArea": 1
},
{
"$type": "UWP.Model.Management.VirtualCarPark.Item.Item, UWP.Model",
"IsPublished": true,
"UId": 45,
"OwnerUId": 41,
"Name": "4: Root - Switches K3 Switch 4",
"UIdStreet": 1,
"UIdArea": 1
}
]
}
]
如你所见,我输入了类型,谢谢
TypeNameHandling = TypeNameHandling.Auto
所以,我不明白为什么我不能反序列化如下
var lstAggregatorItem = JsonConvert.DeserializeObject<List<AggregatorItem>>(tokenAggregators.ToString());
class AggregatorItem
只有接口,none Item 的实现,因为我只想使用接口而不是直接实现
public class AggregatorItem : IAggregatorItem
{
public int UId { get; set; }
public string Name { get; set; }
public bool IsPublished
{
get { return Items.Any(i => i.IsPublished); }
}
public AggregatorItem()
{
Items = new List<IItem>();
}
public IList<IItem> Items { get; set; }
public IItem GetItem(int itemUId)
{
return Items.SingleOrDefault(i => i.UId == itemUId);
}
public void Remove(int itemUId)
{
var itemToRemove = Items.SingleOrDefault(i => i.UId == itemUId);
if (itemToRemove == null)
return;
itemToRemove.Reset();
Items.Remove(itemToRemove);
}
public void AddItem(IItem item)
{
Items.Add(item);
}
public void Reset()
{
foreach (var item in Items)
{
item.Reset();
}
Items.Clear();
}
}
那么,有没有什么东西可以教序列化器“使用这个 class 的这个实现”?因为这个异常让我很烦
Newtonsoft.Json.JsonSerializationException: 'Could not create an instance of type UWP.Model.Management.VirtualCarPark.Item.IItem. Type is an interface or abstract class and cannot be instantiated. Path '[0].Items[0].IsPublished', line 10, position 22.
您必须在此处传递“模型”而不是“AggregatorItem”:
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Item
{
[JsonProperty("$type")]
public string Type { get; set; }
public bool IsPublished { get; set; }
public int UId { get; set; }
public int OwnerUId { get; set; }
public string Name { get; set; }
public int UIdStreet { get; set; }
public int UIdArea { get; set; }
}
public class Root
{
[JsonProperty("$type")]
public string Type { get; set; }
public int UId { get; set; }
public string Name { get; set; }
public bool IsPublished { get; set; }
public List<Item> Items { get; set; }
}
var lstAggregatorItem = JsonConvert.DeserializeObject<List<Root>>(tokenAggregators.ToString());
我已经把其他问题都浏览透了,但我无法解决这个问题。
下面的JSON
[
{
"$type": "UWP.Model.Management.VirtualCarPark.Item.AggregatorItem, UWP.Model",
"UId": 17,
"Name": "K2 BDB-INCON4-U",
"IsPublished": true,
"Items": [{
"$type": "UWP.Model.Management.VirtualCarPark.Item.Item, UWP.Model",
"IsPublished": true,
"UId": 18,
"OwnerUId": 17,
"Name": "1: Root - I/O Modules K2 Switch 1",
"UIdStreet": 1,
"UIdArea": 1
},
{
"$type": "UWP.Model.Management.VirtualCarPark.Item.Item, UWP.Model",
"IsPublished": true,
"UId": 19,
"OwnerUId": 17,
"Name": "2: Root - I/O Modules K2 Switch 2",
"UIdStreet": 1,
"UIdArea": 1
}
]
},
{
"$type": "UWP.Model.Management.VirtualCarPark.Item.AggregatorItem, UWP.Model",
"UId": 41,
"Name": "K3 SH2INDI424",
"IsPublished": true,
"Items": [{
"$type": "UWP.Model.Management.VirtualCarPark.Item.Item, UWP.Model",
"IsPublished": true,
"UId": 42,
"OwnerUId": 41,
"Name": "1: Root - Switches K3 Switch 1",
"UIdStreet": 1,
"UIdArea": 1
},
{
"$type": "UWP.Model.Management.VirtualCarPark.Item.Item, UWP.Model",
"IsPublished": true,
"UId": 43,
"OwnerUId": 41,
"Name": "2: Root - Switches K3 Switch 2",
"UIdStreet": 1,
"UIdArea": 1
},
{
"$type": "UWP.Model.Management.VirtualCarPark.Item.Item, UWP.Model",
"IsPublished": true,
"UId": 44,
"OwnerUId": 41,
"Name": "3: Root - Switches K3 Switch 3",
"UIdStreet": 1,
"UIdArea": 1
},
{
"$type": "UWP.Model.Management.VirtualCarPark.Item.Item, UWP.Model",
"IsPublished": true,
"UId": 45,
"OwnerUId": 41,
"Name": "4: Root - Switches K3 Switch 4",
"UIdStreet": 1,
"UIdArea": 1
}
]
}
]
如你所见,我输入了类型,谢谢
TypeNameHandling = TypeNameHandling.Auto
所以,我不明白为什么我不能反序列化如下
var lstAggregatorItem = JsonConvert.DeserializeObject<List<AggregatorItem>>(tokenAggregators.ToString());
class AggregatorItem
只有接口,none Item 的实现,因为我只想使用接口而不是直接实现
public class AggregatorItem : IAggregatorItem
{
public int UId { get; set; }
public string Name { get; set; }
public bool IsPublished
{
get { return Items.Any(i => i.IsPublished); }
}
public AggregatorItem()
{
Items = new List<IItem>();
}
public IList<IItem> Items { get; set; }
public IItem GetItem(int itemUId)
{
return Items.SingleOrDefault(i => i.UId == itemUId);
}
public void Remove(int itemUId)
{
var itemToRemove = Items.SingleOrDefault(i => i.UId == itemUId);
if (itemToRemove == null)
return;
itemToRemove.Reset();
Items.Remove(itemToRemove);
}
public void AddItem(IItem item)
{
Items.Add(item);
}
public void Reset()
{
foreach (var item in Items)
{
item.Reset();
}
Items.Clear();
}
}
那么,有没有什么东西可以教序列化器“使用这个 class 的这个实现”?因为这个异常让我很烦
Newtonsoft.Json.JsonSerializationException: 'Could not create an instance of type UWP.Model.Management.VirtualCarPark.Item.IItem. Type is an interface or abstract class and cannot be instantiated. Path '[0].Items[0].IsPublished', line 10, position 22.
您必须在此处传递“模型”而不是“AggregatorItem”:
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Item
{
[JsonProperty("$type")]
public string Type { get; set; }
public bool IsPublished { get; set; }
public int UId { get; set; }
public int OwnerUId { get; set; }
public string Name { get; set; }
public int UIdStreet { get; set; }
public int UIdArea { get; set; }
}
public class Root
{
[JsonProperty("$type")]
public string Type { get; set; }
public int UId { get; set; }
public string Name { get; set; }
public bool IsPublished { get; set; }
public List<Item> Items { get; set; }
}
var lstAggregatorItem = JsonConvert.DeserializeObject<List<Root>>(tokenAggregators.ToString());