Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过JSON.NET反序列化另一个类中的接口列表_C#_Json_Json.net - Fatal编程技术网

C# 通过JSON.NET反序列化另一个类中的接口列表

C# 通过JSON.NET反序列化另一个类中的接口列表,c#,json,json.net,C#,Json,Json.net,我已经浏览了所有其他问题,但我无法解决这个问题 JSON如下所示 [ { "$type": "UWP.Model.Management.VirtualCarPark.Item.AggregatorItem, UWP.Model", "UId": 17, "Name": "K2 BDB-INCON4-U", "IsPu

我已经浏览了所有其他问题,但我无法解决这个问题

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
            }
        ]
    }
]
var lstAggregatorItem = JsonConvert.DeserializeObject<List<AggregatorItem>>(tokenAggregators.ToString());
正如你所看到的,我把字体改成了谢谢

TypeNameHandling = TypeNameHandling.Auto
所以,我不明白为什么我不能反序列化如下

[
    {
        "$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
            }
        ]
    }
]
var lstAggregatorItem = JsonConvert.DeserializeObject<List<AggregatorItem>>(tokenAggregators.ToString());
那么,有什么东西可以教序列化程序“使用这个类的这个实现”吗?因为这个例外让我很恼火

Newtonsoft.Json.JsonSerializationException:'无法创建UWP.Model.Management.VirtualPark.Item.IItem类型的实例。类型是接口或抽象类,无法实例化。路径“[0]。项目[0]。已发布”,第10行,位置22

您必须在此处传递“模型”而不是“聚合项”:

//根myDeserializedClass=JsonConvert.DeserializeObject(myJsonResponse);
公共类项目
{
[JsonProperty(“$type”)]
公共字符串类型{get;set;}
公共bool已发布{get;set;}
公共int-UId{get;set;}
public int OwnerUId{get;set;}
公共字符串名称{get;set;}
公共int-UIdStreet{get;set;}
public int UIdArea{get;set;}
}
公共类根
{
[JsonProperty(“$type”)]
公共字符串类型{get;set;}
公共int-UId{get;set;}
公共字符串名称{get;set;}
公共bool已发布{get;set;}
公共列表项{get;set;}
}
var lstAggregatorItem=JsonConvert.DeserializeObject(tokenAggregators.ToString());

当反序列化此属性时,Newtonsoft API不知道要使用哪个类
公共IList项{get;set;}
,因为您使用的是名为
IItem
的接口,而不是实际的类。Rehaan答案中的相关代码是
公共列表项{get;set;}
。请注意您的代码(您使用了
IItem
和Rehaan
)的不同之处,我理解了,但这是唯一的方法吗?正如我写的那样,我希望在不公开实现的情况下保留接口。如果没有其他聪明的方法,我必须用另一个实现来创建另一个类,而不是interfaces@lunatic84. 对于json结构,您可以只拥有两个额外的类,这样,如果json在以后发生更改,您的agregator类的逻辑就不会中断。要在json类和agregator之间转换,可以使用automapper。如果您想在json相关类中使用接口,可以使用