What serialization is:
Serialization is the preservation of the state of an object (the amount of each property), which is then obtained at the appropriate time.
Serialization consists of two parts: serialization and deserialization. Serialization is the first part of this process that decomposes data into a byte stream for storage in a file or on a network. Deserialization is the opening of a byte stream and refactoring the object. Object serialization not only converts the base data type to a byte representation, but sometimes restores the data. Recovering data requires an object instance that has recovery data.
What are the characteristics of serialization:
If a class can be serialized, its subclasses can also be serialized. member data declared as static and transient types cannot be serialized. Because static represents the state of a class, transient represents the temporary data for the object.
When to use serialization:
One: Object serialization can implement distributed objects.
Two: Object serialization preserves not only the data of an object, but also the data of each object referenced by the object. You can write the entire object hierarchy to a stream of bytes that can be saved in a file or passed on a network connection. Object serialization allows you to "deep copy" the object, which is to copy the object itself and the referenced object itself. Serializing an object may get the entire sequence of objects.
1. New class Hotareajson, set the JSON format you want in the inside
1 Public classHotareajson2 {3 Public stringModuletype {Get;Set; }4 5 Public stringImgurl {Get;Set; }6 7 Publiclist< Area> arealist {Get;Set; }8 9 Public classArea {Ten Public stringArealink {Get;Set; } One Public intcrop_x {Get;Set; } A Public intcrop_y {Get;Set; } - Public intCrop_w {Get;Set; } - Public intCrop_h {Get;Set; } the }16
2. Instantiation of Hotareajson class in controller
1Hotareajson area =NewHotareajson ();2 3Area.imgurl ="";4Area.arealist =NewList();5Area.moduletype ="Hotarea";6 7 stringModuledata = ObjToJson1 (area);//convert to JSON string8 9 //single object to JSON methodTen Public stringObjtojson<t>(T data) One { AJavaScriptSerializer JSS =NewJavaScriptSerializer (); - stringJsonstr =JSS. Serialize (data); - returnJsonstr; the}
3. Deserializing the data and re-copying the Save
1 vardata = newtonsoft.json.jsonconvert.deserializeobject//Deserialize the data to be saved2 3Hotareajson.area area =NewHotareajson.area ();4 5Area. crop_x =1111;6Area. Crop_y =2222;7Area. Crop_w =3333;8Area. Crop_h =4444;9Area. Arealink ="www.baidu.com";Ten OneModuledata = Objtojson (data);//convert to JSON string
Code for the simplified, only for notes, what is the problem to point out together progress, a lot of advice!
Simple JSON serialization and deserialization