using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Runtime.Serialization.Json; using System.IO; using System.Text; using System.Web.Script.Serialization; using System.Data; /// <summary> /// JSON序列化和反序列化辅助类 /// </summary> public class JsonHelper { /// <summary> /// 序列化方法(带分页) /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string Serialize(DataTable dt) { List<Dictionary<string, object>> list = new List<Dictionary<string, object>>(); foreach (DataRow dr in dt.Rows) { Dictionary<string, object> result = new Dictionary<string, object>(); foreach (DataColumn dc in dt.Columns) { result.Add(dc.ColumnName, dr[dc].ToString()); } list.Add(result); } int count = 0; try { count = Convert.ToInt32(dt.TableName); } catch (System.Exception ex) { count = dt.Rows.Count; } string strReturn = ""; if (count == 0) { strReturn = "{'totalCount':0,'data':[]}"; } else { strReturn = ConventToJson(list, count); } return strReturn; } /// <summary> /// 转换为JSON对象 /// </summary> /// <returns></returns> public static string ConventToJson<T>(List<T> list, int count) { JavaScriptSerializer serializer = new JavaScriptSerializer(); string strJson = serializer.Serialize(list); strJson = strJson.Substring(1); strJson = strJson.Insert(0, "{totalCount:" + count + ",data:["); strJson += "}"; return strJson; } /// <summary> /// 不需要分页 /// </summary> /// <param name="dt"></param> /// <param name="flag">false</param> /// <returns></returns> public static string Serialize(DataTable dt, bool flag) { JavaScriptSerializer serializer = new JavaScriptSerializer(); List<Dictionary<string, object>> list = new List<Dictionary<string, object>>(); foreach (DataRow dr in dt.Rows) { Dictionary<string, object> result = new Dictionary<string, object>(); foreach (DataColumn dc in dt.Columns) { result.Add(dc.ColumnName, dr[dc].ToString()); } list.Add(result); } return serializer.Serialize(list); ; } /// <summary> /// JSON序列化 /// </summary> public static string JsonSerializer<T>(T t) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); MemoryStream ms = new MemoryStream(); ser.WriteObject(ms, t); string jsonString = Encoding.UTF8.GetString(ms.ToArray()); ms.Close(); return jsonString; } /// <summary> /// JSON反序列化 /// </summary> public static T JsonDeserialize<T>(string jsonString) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)); T obj = (T)ser.ReadObject(ms); return obj; } }