Here is the case that the key value of the dictionary is a custom class, and other cases are not in the scope of the discussion, and the Newtonsoft.Json.dll used is given at the end.
Gossip less, directly on the code.
Using system;using system.collections.generic;namespace test{ class KeyClass { public int Value; public keyclass (Int value) { this.value = value; } public static explicit operator keyclass (string jsonString) { return Newtonsoft.Json.JsonConvert.DeserializeObject<KeyClass> (jsonstring); } public override String tostring () &NBSP;&NBSP;&Nbsp; { Return newtonsoft.json.jsonconvert.serializeobject (This); } } class program { static void main (String[] args) { testdictionaryjson (); } static public void Testdictionaryjson () { dictionary<keyclass, list<keyclass>> dic = new dictionary<keyclass,&nbSp List<keyclass>> (); keyclass key1 = new keyclass (1); List<KeyClass> value1 = new List<KeyClass> (); value1. AddRange (New keyclass[] { new keyclass (111), new keyclass (, new ) Keyclass (113) }); keyclass Key2 = new keyclass (2); List<keyclass> value2 = new list<keyclass> (); value2. AddRange (New keyclass[] { new keyclass (211), new keyclass (212), new Keyclass (213) }); dic. ADD (key1, value1); dic. ADD (key2, value2); string str = newtonsoft.json.jsonconvert.serializeobject (DIC); var result = Newtonsoft.Json.JsonConvert.DeserializeObject< Dictionary<keyclass, list<keyclass>>> (str); } }}
Among the most important are:
public static explicit operator Keyclass (string jsonstring) {return newtonsoft.json.jsonconvert.deserializeobject< Keyclass> (jsonstring);} public override string ToString () {return Newtonsoft.Json.JsonConvert.SerializeObject (this);}
Description There are many things in the Newtonsoft.Json.dll, but I am not familiar with it; so far, the general serialization and deserialization of using it is problematic, but it does not preclude the use of other usages that are entirely competent; the above is a set of methods that I have summed up, welcome you crossing to other more effective The method.
Dll:https://github.com/jamesnk/newtonsoft.json
This article is from the "keepmoving" blog, make sure to keep this source http://maqqme.blog.51cto.com/9277946/1730149
C # Json dictionary serialization and serialization