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