Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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以列出<;MyObject<;T、 K>&燃气轮机;使用JSON.Net_C#_Generics_Serialization_Json.net - Fatal编程技术网

C# 是否可以反序列化JSON以列出<;MyObject<;T、 K>&燃气轮机;使用JSON.Net

C# 是否可以反序列化JSON以列出<;MyObject<;T、 K>&燃气轮机;使用JSON.Net,c#,generics,serialization,json.net,C#,Generics,Serialization,Json.net,我有一门课: [Serializable] public class KVPair<TKey, TValue> { public TKey Key { get; set; } public TValue Value { get; set; } public KVPair(TKey k, TValue v) { Key = k; Value = v;

我有一门课:

[Serializable]
    public class KVPair<TKey, TValue>
    {
        public TKey Key { get; set; }
        public TValue Value { get; set; }

        public KVPair(TKey k, TValue v)
        {
            Key = k;
            Value = v;
        }
    }    

当我将这个字符串反序列化回List>时,我得到了正确的对象计数,但它们是空的。任何建议都会很有帮助。

我猜您可能需要添加一个无参数构造函数:

public KVPair() {
}

也许JSON.net不知道如何构造对象,因此它会自动失败。

您知道已经存在KeyValuePair类型吗?请检查此问题,因为使用Web服务序列化KeyValuePair时存在问题:
"[{\"Key\":\"Two\",\"Value\":\"2\"},{\"Key\":\"One\",\"Value\":\"1\"}]"
public KVPair() {
}