Is it possible to deserialize JSON for the list <MyObject <T, K >> using JSON.Net

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

which I create:

List<KVPair<string,string>> kvPairs;

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

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

When I de-serialize this line back to List> I get the correct number of objects, but they are zero. Any suggestions would be a big help.

+3
source share
1 answer

I assume that you might need to add a constructor without parameters:

public KVPair() {
}

Perhaps JSON.net does not know how to build your object so that it is silent.

+4
source

Source: https://habr.com/ru/post/1728852/


All Articles