开发者

Is it possible to deserialize JSON to List<MyObject<T,K>> with JSON.Net

开发者 https://www.devze.com 2022-12-17 11:04 出处:网络
I have a class: [Serializable] public class KVPair<TKey, TValue> { public TKey Key { get; set; } public TValue Value { get; set; }

I have a class:

[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;
        }
    }    

that I create:

List<KVPair<string,string>> kvPairs;

Using the JSON.Net library I can serialize the list and yield:

"[{\"Key\":\"Two\",\"Value\":\"2\"},{\"Key\":\"One\",\"Value\":\"1\"}]"

When I de-serialize this string back to List> I get the correct count of objects开发者_运维知识库 but they are null. Any suggestions would be great help.


I'm guessing that you might need to add a parameterless constructor:

public KVPair() {
}

Perhaps JSON.net doesn't know how to construct your object so it silently fails.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号