The conversion between serialization and deserialization in several ways is described here
The first one describes how to serialize, and the object objects are serialized in two common ways, namely, string and XML objects;
The first is to convert an object to a string object, which is relatively simple and there is nothing to talk about;
public string scriptserialize<t> (T t) { JavaScriptSerializer serializer = new JavaScriptSerializer (); Return serializer. Serialize (t); }
The second converts an object to an XML object:
public string scriptserializetoxml<t> (T-t) { XmlSerializer serializer = new XmlSerializer (typeof (T)); MemoryStream mem = new MemoryStream (); XmlTextWriter writer = new XmlTextWriter (MEM,ENCODING.UTF8); XmlSerializerNamespaces ns = new XmlSerializerNamespaces (); Ns. Add ("", ""); Serializer. Serialize (Writer,t,ns); Writer. Close (); Return Encoding.UTF8.GetString (Mem. ToArray ()); }
Here I mainly talk about string objects deserialized into corresponding objects;
One, deserializes a string object into an object
Public T scriptdeserialize<t> (string Strjson) { JavaScriptSerializer serializer = new JavaScriptSerializer (); Return serializer. Deserialize<t> (Strjson); }
Second, deserialize a string object into a list object
Public list<t> jsonstringtolist<t> (string Strjson) { JavaScriptSerializer serializer = new JavaScriptSerializer (); List<t> objlist = serializer. Deserialize<list<t>> (Strjson); return objlist; }
Third, deserialize a string object into a DataTable object
Public DataTable jsonstringtodatatable<t> (string strjson) {DataTable dt = new DataTable (); if (Strjson.indexof ("[") >-1)//If greater than Strjson holds multiple model objects {Strjson = Strjson.remove (s Trjson.length-1, 1). Remove (0, 1). Replace ("},{", "}; {"); } JavaScriptSerializer Serializer = new JavaScriptSerializer (); string[] items = Strjson.split (';'); foreach (PropertyInfo property in typeof (T). GetProperties ())//All properties of type T are obtained by reflection {DataColumn col = new DataColumn (property). Name,property. PropertyType); Dt. Columns.Add (COL); }//Loop One-by-one deserialization for (int i = 0; i < items. Length; i++) {DataRow dr = dt. NewRow (); Deserializes to a T-type Object T temp = serializer. Deserialize<t> (Items[i]); foreach (PropertyInfo property in typeof (T). GetProperties ()) { Dr[property. Name] = property. GetValue (Temp,null); } dt. Rows.Add (DR); } return DT; }
Iv. deserializing an XML object into an object
Public T jsonxmltoobject<t> (string Strjson) { XmlDocument xdoc = new XmlDocument (); Try { Xdoc. LOADXML (Strjson); XmlNodeReader reader = new XmlNodeReader (Xdoc. documentelement); XmlSerializer ser = new XmlSerializer (typeof (T)); Object obj = ser. Deserialize (reader); Return (T) obj; Catch { return default (T); } }
Now how do you call them with concrete examples? In particular, it is important to note that the XML object is deserialized Objcet object
public class Loginobject {public string account {get; set;} public string Password {get; set;} }
1 Loginobject loginobject = new Loginobject {account = account, Password = Password}; 2 Extools.manag E.class.cscriptserialize Serialize = new Class.cscriptserialize (); 3//Convert Object object to string 4 string strjson=serialize.scriptserialize (Loginobject); 5 6//Convert Object object to XML object 7 string Strjson = Serialize.scriptserializetoxml (Loginobject); 8 9 10//Convert to List object List<loginobject> list = Serialize.jsonstringtolist<lo Ginobject> (Strjson); 12//Convert an XML object to object Strjson = strjson.substring (1, strjson.length-1 ); loginobject = serialize.jsonxmltoobject<loginobject> (Strjson); 15//Convert string to DataTable16 DataTable dt = serialize.jsonstringtodatatable<loginobject> (Strjson); 17//Convert string to object 18 Loginobject = serialize.scriptdeserialize<loginobject> (Strjson);
C # serialization vs. deserialization conversion of several formats