C#序列化与反序列化⼏种格式的转换这⾥介绍了⼏种⽅式之间的序列化与反序列化之间的转换
⾸先介绍的如何序列化,将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类型对象
json转换对象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);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论