序列化和反序列化
using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Web; using System.Web.Script.Serialization; using System.Web.UI; using System.Web.UI.WebControls; using System.Xml; using System.Xml.Serialization; namespace test { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { LoginObject loginObject = new LoginObject { Account = "song", Password = "1111" }; //将object对象转换为string string strJson = ScriptSerialize(loginObject); //将object对象转换为xml对象 string strJson1 = ScriptSerializeToXML(loginObject); //转换为list对象 List<LoginObject> list = JSONStringToList<LoginObject>(strJson); //将一个xml对象转换为object对象 string strJson2 = strJson.Substring(1, strJson.Length - 1); loginObject = JSONXMLToObject<LoginObject>(strJson1); //将字符串转换为dataTable DataTable dt = JSONStringToDataTable<LoginObject>(strJson); //将字符串转换为object对象 loginObject = ScriptDeserialize<LoginObject>(strJson); } public string ScriptSerialize<T>(T t) { JavaScriptSerializer serializer = new JavaScriptSerializer(); return serializer.Serialize(t); } 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()); } public List<T> JSONStringToList<T>(string strJson) { JavaScriptSerializer serializer = new JavaScriptSerializer(); List<T> objList = serializer.Deserialize<List<T>>(strJson); return objList; } public T ScriptDeserialize<T>(string strJson) { JavaScriptSerializer serializer = new JavaScriptSerializer(); return serializer.Deserialize<T>(strJson); } 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); } } 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; } } public class LoginObject { public string Account { get; set; } public string Password { get; set; } } }