Serialization and deserialization of JSON in C #
JSON is a common data format that is often used during data exchange. The following describes JSON serialization and deserialization in C #. Of course, JSON can also be used in Asp.net, Silverlight, in WPF. The following example describes how to serialize and deserialize JSON. This article describes two solutions:. Net 3.5 native JSON operations and JSON operations in json.net.
First, create a test object for serialization and deserialization.
Public class person {public int ID {Get; set;} public string name {Get; set;} public int age {Get; set;} public datetime birthday {Get; set ;}}
First, use. net Framework 3.5, the original JSON operation class library, features that it does not need to be imported into a third-party class library, which is convenient, fast, and required. net Framework support, we know that there are still many servers not upgraded.. NET Framework 3.5.
First, import the following class library and add the followingCode
System. servicemodel
System. servicemodel. Web
Class program {static void main (string [] ARGs) {// create the test object person P = new person (); p. id = 1; p. name = "James"; p. age = 20; p. birthday = datetime. now. addyears (-20); // converts an object to a JSON string datacontractjsonserializer DS = new datacontractjsonserializer (typeof (person); Using (memorystream MS = new memorystream () {Ds. writeobject (MS, P); string output = encoding. utf8.getstring (Ms. toarray (); console. writeline (output );}}}
Output result
This is the native serialization method of. NET Framework 3.5. Next, we will introduce the deserialization method and add the following code.
Class program {static void main (string [] ARGs) {// create the test object person P = new person (); p. id = 1; p. name = "James"; p. age = 20; p. birthday = datetime. now. addyears (-20); // converts an object to a JSON string output = string. empty; datacontractjsonserializer DS = new datacontractjsonserializer (typeof (person); Using (memorystream MS = new memorystream () {Ds. writeobject (MS, P); Output = encoding. utf8.getstring (Ms. toarray ();} // convert the JSON string to the datacontractjsonserializer outds = new datacontractjsonserializer (typeof (person); Using (memorystream outms = new memorystream (encoding. utf8.getbytes (output) {person outperson = outds. readobject (outms) as person; console. writeline ("ID:" + outperson. ID); console. writeline ("name:" + outperson. name); console. writeline ("Age:" + outperson. age); console. writeline ("birthday:" + outperson. birthday );}}}
Output result
We can see from the native JSON serialization and deserialization methods of. NET Framework 3.5, it is still quite troublesome. Many netizens will also ask what to do if it is. NET Framework 2.0? Next, we will introduce a third-party JSON serialization and deserialization class library to complete the above operations. This class library is available. net Framework 2.0, and this class library can also support XML serialization and deserialization operations (this article will not explain ). See the operation below.
Class program {static void main (string [] ARGs) {// create the test object person P = new person (); p. id = 1; p. name = "James"; p. age = 20; p. birthday = datetime. now. addyears (-20); // converts an object to a JSON string output = jsonconvert. serializeobject (p); console. writeline (output );}}
Output result
This is a JSON serialization method. Is it much simpler?
Class program {static void main (string [] ARGs) {// create the test object person P = new person (); p. id = 1; p. name = "James"; p. age = 20; p. birthday = datetime. now. addyears (-20); // converts an object to a JSON string output = jsonconvert. serializeobject (p); // converts a JSON string to the object person outperson = jsonconvert. deserializeobject <person> (output); console. writeline ("ID:" + outperson. ID); console. writeline ("name:" + outperson. name); console. writeline ("Age:" + outperson. age); console. writeline ("birthday:" + outperson. birthday );}}
Output result
This is the JSON deserialization method. It is simple. It is said that this class library is much faster than the native JSON serialization and deserialization methods of. net. Http://json.codeplex.com/