C # Json dictionary serialization and serialization

Source: Internet
Author: User

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.


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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.