using System; using System.Data; using System.Text; using System.IO; using Newtonsoft.Json; using System.Collections.Generic; using System.Web.Script.Serialization; using System.Collections; namespace DSWeb.TruckMng.Helper { public class JsonHelper { public static T Deserialize(string json) { T obj = Activator.CreateInstance(); obj = Newtonsoft.Json.JsonConvert.DeserializeObject(json); return obj; } public static string Serialize(T obj) { string retVal = string.Empty; retVal = Newtonsoft.Json.JsonConvert.SerializeObject(obj); return retVal; } #region 转化Dataset or DataTable #region 直接转为Json字符串 public static string DataTableToJSON(DataTable dt, string dtName) { StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); using (JsonWriter jw = new JsonTextWriter(sw)) { JsonSerializer ser = new JsonSerializer(); jw.WriteStartObject(); jw.WritePropertyName(dtName); jw.WriteStartArray(); foreach (DataRow dr in dt.Rows) { jw.WriteStartObject(); foreach (DataColumn dc in dt.Columns) { jw.WritePropertyName(dc.ColumnName); ser.Serialize(jw, dr[dc].ToString()); } jw.WriteEndObject(); } jw.WriteEndArray(); jw.WriteEndObject(); sw.Close(); jw.Close(); } return sb.ToString(); } #endregion #region 转化为Controller中可以使用Json(new {..})使用的方式 #region 例子 //public static Dictionary getTable() //{ // string sql = "select user_name, active_indicator, create_date from users"; // string connString = "database=db; server=localhost; user id=sa;"; // return JsonMethods.ToJson(GetDataTable(sql, connString)); //} //private static DataTable GetDataTable(string sql, string connString) //{ // using (SqlConnection myConnection = new SqlConnection(connString)) // { // using (SqlCommand myCommand = new SqlCommand(sql, myConnection)) // { // myConnection.Open(); // using (SqlDataReader myReader = myCommand.ExecuteReader()) // { // DataTable myTable = new DataTable(); // myTable.TableName = "mydt"; // myTable.Load(myReader); // myConnection.Close(); // return myTable; // } // } // } //} //Controller中 return Json(new {data=getTable().ToArray()}); #endregion private static List> RowsToDictionary(DataTable table) { List> objs = new List>(); if (table == null) return objs; foreach (DataRow dr in table.Rows) { Dictionary drow = new Dictionary(); for (int i = 0; i < table.Columns.Count; i++) { drow.Add(table.Columns[i].ColumnName, dr[i]); } objs.Add(drow); } return objs; } /// /// 把DataTable转化为Controller可以转为Json数据的数据集合 /// /// 需要转换的DataTable /// public static List> ToJson(DataTable table) { return RowsToDictionary(table); } //public static Dictionary ToJson(DataTable table) //{ // Dictionary dict = new Dictionary(); // dict.Add(table.TableName, RowsToDictionary(table)); // return dict; //} /// /// 把DataSet转化为Controller可以转为Json数据的数据集合 /// /// 需要转换的DataSet /// public static Dictionary ToJson(DataSet data) { Dictionary dict = new Dictionary(); foreach (DataTable table in data.Tables) { dict.Add(table.TableName, RowsToDictionary(table)); } return dict; } #endregion #endregion static public object JsonGetValue(string jsondata,string jsonname) { JavaScriptSerializer serializer = new JavaScriptSerializer(); Dictionary json = (Dictionary)serializer.DeserializeObject(jsondata); object value; if (!json.TryGetValue(jsonname, out value)) { throw new Exception("数据中不存在此值!"); } return value; } /// /// 获取JsonString /// /// DataSet /// public static string GetJsonString(DataSet ds) { string res = ""; ; IList mList = new List(); try { foreach (DataRow row in ds.Tables[0].Rows) { Hashtable ht = new Hashtable(); foreach (DataColumn col in ds.Tables[0].Columns) { ht.Add(col.ColumnName, row[col.ColumnName]); } mList.Add(ht); } res = Newtonsoft.Json.JsonConvert.SerializeObject(mList); } catch (Exception ee) { string error = ee.Message; } return res; } } }