Serialization and deserialization of JSON

Source: Internet
Author: User

For JSON serialization and deserialization, if you write your own source code to implement, it is very complicated, so I used to use someone else has written a reference file. There are many such files online, I use Litjson, of course newtonsoft can, but the latter need to write a class for the data structure to undertake the pre-serialization and deserialization of the data.

First download the Litjson library file from the Internet, you can also find Me (QQ2422082778). The Litjson DLL file is then referenced in the project, in Litjson-0.7.0\bin,litjson such as:

And then we can use it. In order to better use it when writing code, we introduce it to the code file that needs to be used: using Litjson;.

To better illustrate serialization and deserialization, I've built a product class

1 namespacejsontest2 {3     classProduct4     {5          Public stringname;6          PublicDateTime expiry;7          Public string[] sizes=New string[3];8     }9}

1. Serialization

1 //Set Object2Product product=NewProduct ();3Product.name ="Apple";4Product.expiry =NewDateTime ( the, A,6, -, A, A);5product.sizes =New string[] {"Small","Medium","Large" };6 //Serialization of7Jsondata xx=Jsonmapper.tojson (product);8Console.WriteLine (xx. ToString ());//Convert to String9Console.WriteLine (xx. ToJson ());//A string converted to a JSON type

Serialization results

2. Deserialization

1 //Set Object2Product product=NewProduct ();3Product.name ="Apple";4Product.expiry =NewDateTime ( the, A,6, -, A, A);5product.sizes =New string[] {"Small","Medium","Large" };6 //deserialization7Jsondata JD =jsonmapper.toobject (output);8Console.WriteLine (jd["name"]. ToString ());//gets the value of the "name" property based on the property name

Deserialization results

It's simple, right! Just one or two lines of code, and that's how easy it is to Json,json. But if you want to learn more about JSON, it takes a bit of thought.

Serialization and deserialization of 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.