這里介紹了幾種方式之間的序列化與反序列化之間的轉(zhuǎn)換
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、微信小程序、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了義安免費(fèi)建站歡迎大家使用!首先介紹的如何序列化,將object對(duì)象序列化常見(jiàn)的兩種方式即string和xml對(duì)象;
第一種將object轉(zhuǎn)換為string對(duì)象,這種比較簡(jiǎn)單沒(méi)有什么可談的;
public string ScriptSerialize<T>(T t)
{
JavaScriptSerializer serializer= new JavaScriptSerializer();
return serializer.Serialize(t);
}
第二種將object轉(zhuǎn)換為xml對(duì)象:
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對(duì)象反序列化為對(duì)應(yīng)的對(duì)象;
一、將string對(duì)象反序列化為object對(duì)象
public T ScriptDeserialize<T>(string strJson)
{
JavaScriptSerializer serializer= new JavaScriptSerializer();
return serializer.Deserialize<T>(strJson);
}
二、將string對(duì)象反序列化為list對(duì)象
public List<T> JSONStringToList<T>(string strJson)
{
JavaScriptSerializer serializer= new JavaScriptSerializer();
List<T> objList = serializer.Deserialize<List<T>>(strJson);
return objList;
}
三、將string對(duì)象反序列化為datatable對(duì)象
public DataTable JSONStringToDataTable<T>(string strJson)
{
DataTable dt= new DataTable();
if (strJson.IndexOf("[") > -1)//如果大于則strJson存放了多個(gè)model對(duì)象 {
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())//通過(guò)反射獲得T類型的所有屬性 {
DataColumn col= new DataColumn(property.Name,property.PropertyType);
dt.Columns.Add(col);
}
//循環(huán) 一個(gè)一個(gè)的反序列化 for (int i = 0; i < items.Length; i++)
{
DataRow dr= dt.NewRow();
//反序列化為一個(gè)T類型對(duì)象 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對(duì)象反序列化為object對(duì)象
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);
}
}
現(xiàn)在用具體的實(shí)例來(lái)如何調(diào)用他們呢?特別要注意的是將xml對(duì)象反序列化objcet對(duì)象
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對(duì)象轉(zhuǎn)換為string 4 string strJson=Serialize.ScriptSerialize(loginObject);
5
6 //將object對(duì)象轉(zhuǎn)換為xml對(duì)象 7 string strJson = Serialize.ScriptSerializeToXML(loginObject);
8
9
10 //轉(zhuǎn)換為list對(duì)象11 List<LoginObject> list = Serialize.JSONStringToList<LoginObject>(strJson);
12 //將一個(gè)xml對(duì)象轉(zhuǎn)換為object對(duì)象13 strJson = strJson.Substring(1, strJson.Length - 1);
14 loginObject = Serialize.JSONXMLToObject<LoginObject>(strJson);
15 //將字符串轉(zhuǎn)換為dataTable16 DataTable dt = Serialize.JSONStringToDataTable<LoginObject>(strJson);
17 //將字符串轉(zhuǎn)換為object對(duì)象18 loginObject = Serialize.ScriptDeserialize<LoginObject>(strJson);
因本人技術(shù)有限,現(xiàn)階段僅用作記錄收藏他人作品。
網(wǎng)站標(biāo)題:序列化與反序列化之間的轉(zhuǎn)換-創(chuàng)新互聯(lián)
文章來(lái)源:http://jinyejixie.com/article32/dehpsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開(kāi)發(fā)、標(biāo)簽優(yōu)化、App設(shè)計(jì)、網(wǎng)站維護(hù)、關(guān)鍵詞優(yōu)化、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容