How to implement serialization to JSON

Source: Internet
Author: User

Because of the need to observe the object's data, so look for the description class that can automatically describe the field name and its value in the object, consulted a lot of people, it is recommended to use JSON, but JSON used to have a little extra action.

Need to reference in file

Using System.Web.Script.Serialization;

But added the discovery prompt error, cannot find the space name, originally also needs to add the reference in the project reference:

I'm using VS2010, I can't find it in a. NET reference, I'm looking for information, and I'm going to go to the C drive and add it. The address I found is as follows:

After adding a reference, you can use it when you add it to your browser.

         Public classTest//Custom data class {//Public dictionary<int, int> dary = new Dictionary<int, int> ();             Publiclist<list<int>> Sary =Newlist<list<int>>();  Publiclist<int> nary =Newlist<int>();  PublicXXX mx =Newxxx ();  PublicWori WR =NewWori ();  Publictuple<int,int,int,string> MT =Newtuple<int,int,int,string> (1,1,1,"AA");  PublicTest () {int[] Nnary =New int[5] {1,2,3, $,7 }; Nary=Newlist<int>(nnary); Sary=Newlist<list<int>>(); Sary. ADD (ZCL. Tbas.MEarray.CreatIntRand (Ten,1, +)); Sary. ADD (ZCL. Tbas.MEarray.CreatIntRand (Ten,1, +)); Sary. ADD (ZCL. Tbas.MEarray.CreatIntRand (Ten,1, +)); Sary. ADD (ZCL. Tbas.MEarray.CreatIntRand (Ten,1, +)); Sary. ADD (ZCL. Tbas.MEarray.CreatIntRand (Ten,1, +)); Sary. ADD (ZCL. Tbas.MEarray.CreatIntRand (Ten,1, +)); }        }
            new test ();  // Implemented by the following code            New JavaScriptSerializer ();             New StringBuilder ();            Serializer. Serialize (MD2, SB);            ME. Msgshow (sb.) ToString ());

The final result is:

{"Sary": [[950,652,375,867,923,494,271,923,897,522],[624,631,8,505,671,508,888,827,19,983],[ 478,623,576,323,457,735,776,670,528,497],[237,813,868,350,901,512,303,135,374,808],[ 656,670,553,478,83,837,759,501,409,409],[348,473,931,859,751,328,76,53,411,189]], "nary": [1,2,3,45,7], "mx": {"Ma ":", "MB": +, "sa": "abc", "SB": "33333", "SC": "EWR"}, "WR": {"MD": 0, "Ma": 0, "sa": null, "SB": null, "SC": null}, "MT": {" Item1 ": 1," Item2 ": 1," Item3 ": 1," Item4 ":" AA "}}

Note: JSON is not able to serialize dictionary directly, it needs to be converted to list.

How to implement serialization to JSON

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.