繁体   English   中英

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只有接口,没有 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();
    }
}

那么,有什么东西可以教给序列化程序“使用这个类的这个实现”吗? 因为这个异常让我很烦

Newtonsoft.Json.JsonSerializationException: '无法创建 UWP.Model.Management.VirtualICarPark 类型的实例类型是接口或抽象 class 并且不能被实例化。 路径“[0].Items[0].IsPublished”,第 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());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM