XML和JSON 序列化以及DataTable转JSON

using System.IO;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.Runtime.Serialization.Json; namespace Common.Core
{
public class SerializationHelper
{
private static string XmlSerialize(object o)
{
XmlSerializer ser = new XmlSerializer(o.GetType());
System.IO.MemoryStream mem = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(mem, Encoding.UTF8);
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
ser.Serialize(writer, o, ns);
writer.Close();
return Encoding.UTF8.GetString(mem.ToArray());
} private static T XmlDeserialize<T>(string s)
{
XmlDocument xdoc = new XmlDocument();
try
{
xdoc.LoadXml(s);
XmlNodeReader reader = new XmlNodeReader(xdoc.DocumentElement);
XmlSerializer ser = new XmlSerializer(typeof(T));
object obj = ser.Deserialize(reader);
return (T)obj;
}
catch
{
return default(T);
}
} private static string JsonSerialize(object o)
{
using (var ms = new MemoryStream())
{
new DataContractJsonSerializer(o.GetType()).WriteObject(ms, o);
return Encoding.UTF8.GetString(ms.ToArray());
}
} private static T JsonDeserialize<T>(string s)
{
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(s)))
{
return (T)new DataContractJsonSerializer(typeof(T)).ReadObject(ms);
}
} /// <summary>
/// 将对象根据格式(XML/JSON)序列化成字符串结果
/// </summary>
/// <param name="o">目标对象</param>
/// <param name="format">输出格式</param>
/// <returns></returns>
public static string Serialize(object o, Format format)
{
if (format == Format.Xml)
{
return SerializationHelper.XmlSerialize(o);
}
else
{
return SerializationHelper.JsonSerialize(o);
}
} /// <summary>
/// 将字符串根据格式(XML/JSON)反序列化成指定类型的对象
/// </summary>
/// <typeparam name="T">指定类型</typeparam>
/// <param name="s">目标字符串</param>
/// <param name="format">输入格式</param>
/// <returns></returns>
public static T Deserialize<T>(string s, Format format)
{
if (format == Format.Xml)
{
return SerializationHelper.XmlDeserialize<T>(s);
}
else
{
return SerializationHelper.JsonDeserialize<T>(s);
}
}
}
}
public class DataTableConvertJson
{ #region dataTable转换成Json格式
/// <summary>
/// dataTable转换成Json格式
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static string DataTable2Json(DataTable dt)
{
StringBuilder jsonBuilder = new StringBuilder();
jsonBuilder.Append("{\"");
jsonBuilder.Append(dt.TableName);
jsonBuilder.Append("\":[");
jsonBuilder.Append("[");
for (int i = ; i < dt.Rows.Count; i++)
{
jsonBuilder.Append("{");
for (int j = ; j < dt.Columns.Count; j++)
{
jsonBuilder.Append("\"");
jsonBuilder.Append(dt.Columns[j].ColumnName);
jsonBuilder.Append("\":\"");
jsonBuilder.Append(dt.Rows[i][j].ToString());
jsonBuilder.Append("\",");
}
jsonBuilder.Remove(jsonBuilder.Length - , );
jsonBuilder.Append("},");
}
jsonBuilder.Remove(jsonBuilder.Length - , );
jsonBuilder.Append("]");
jsonBuilder.Append("}");
return jsonBuilder.ToString();
} #endregion dataTable转换成Json格式
#region DataSet转换成Json格式
/// <summary>
/// DataSet转换成Json格式
/// </summary>
/// <param name="ds">DataSet</param>
/// <returns></returns>
public static string Dataset2Json(DataSet ds)
{
StringBuilder json = new StringBuilder(); foreach (DataTable dt in ds.Tables)
{
json.Append("{\"");
json.Append(dt.TableName);
json.Append("\":");
json.Append(DataTable2Json(dt));
json.Append("}");
} return json.ToString();
}
#endregion /// <summary>
/// Msdn
/// </summary>
/// <param name="jsonName"></param>
/// <param name="dt"></param>
/// <returns></returns>
public static string DataTableToJson(string jsonName, DataTable dt)
{
StringBuilder Json = new StringBuilder();
Json.Append("{\"" + jsonName + "\":[");
if (dt.Rows.Count > )
{
for (int i = ; i < dt.Rows.Count; i++)
{
Json.Append("{");
for (int j = ; j < dt.Columns.Count; j++)
{
Json.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":\"" + dt.Rows[i][j].ToString() + "\"");
if (j < dt.Columns.Count - )
{
Json.Append(",");
}
}
Json.Append("}");
if (i < dt.Rows.Count - )
{
Json.Append(",");
}
}
}
Json.Append("]}");
return Json.ToString();
}
}

参考:http://www.cnblogs.com/ynbt/archive/2012/12/11/2813911.html

上一篇:SpringBoot整合RabbitMQ-整合演示


下一篇:Centos上安装phpmyadmin