這裡介紹了幾種方式之間的序列化與反序列化之間的轉換
首先介紹的如何序列化,將object對象序列化常見的兩種方式即string和xml對象;
第一種將object轉換為string對象,這種比較簡單沒有什麼可談的;
public string ScriptSerialize<T>(T t) { JavaScriptSerializer serializer = new JavaScriptSerializer(); return serializer.Serialize(t); }
第二種將object轉換為xml對象:
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()); }
下面我主要講string對象反序列化為對應的對象;
一、將string對象反序列化為object對象
public T ScriptDeserialize<T>(string strJson) { JavaScriptSerializer serializer = new JavaScriptSerializer(); return serializer.Deserialize<T>(strJson); }
二、將string對象反序列化為list對象
public List<T> JSONStringToList<T>(string strJson) { JavaScriptSerializer serializer = new JavaScriptSerializer(); List<T> objList = serializer.Deserialize<List<T>>(strJson); return objList; }
三、將string對象反序列化為datatable對象
public DataTable JSONStringToDataTable<T>(string strJson) { DataTable dt = new DataTable(); if (strJson.IndexOf("[") > -1)//如果大於則strJson存放了多個model對象 { strJson = strJson.Remove(strJson.Length - 1, 1).Remove(0, 1).Replace("},{", "};{"); } JavaScriptSerializer serializer = new JavaScriptSerializer(); string[] items = strJson.Split(';'); foreach (PropertyInfo property in typeof(T).GetProperties())//通過反射獲得T類型的所有屬性 { DataColumn col = new DataColumn(property.Name,property.PropertyType); dt.Columns.Add(col); } //循環 一個一個的反序列化 for (int i = 0; i < items.Length; i++) { DataRow dr = dt.NewRow(); //反序列化為一個T類型對象 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; }
四、將xml對象反序列化為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); } }
現在用具體的實例來如何調用他們呢?特別要注意的是將xml對象反序列化objcet對象
public class LoginObject { public string Account { get; set;} public string Password { get; set;} }
1 LoginObject loginObject = new LoginObject { Account = account, Password = password }; 2 ExTools.Manage.Class.CScriptSerialize Serialize = new Class.CScriptSerialize(); 3 //將object對象轉換為string 4 string strJson=Serialize.ScriptSerialize(loginObject); 5 6 //將object對象轉換為xml對象 7 string strJson = Serialize.ScriptSerializeToXML(loginObject); 8 9 10 //轉換為list對象 11 List<LoginObject> list = Serialize.JSONStringToList<LoginObject>(strJson); 12 //將一個xml對象轉換為object對象 13 strJson = strJson.Substring(1, strJson.Length - 1); 14 loginObject = Serialize.JSONXMLToObject<LoginObject>(strJson); 15 //將字符串轉換為dataTable 16 DataTable dt = Serialize.JSONStringToDataTable<LoginObject>(strJson); 17 //將字符串轉換為object對象 18 loginObject = Serialize.ScriptDeserialize<LoginObject>(strJson);