首页 给出 DataTable 转Json 的方法:
1 public static string TableToJson(DataTable dt) 2 { 3 List<Dictionary<string, object>> list = new List<Dictionary<string, object>>(); 4 foreach (DataRow dr in dt.Rows)//每一行信息,新建一个Dictionary<string,object>,将该行的每列信息加入到字典 5 { 6 Dictionary<string, object> result = new Dictionary<string, object>(); 7 foreach (DataColumn dc in dt.Columns) 8 { 9 result.Add(dc.ColumnName, dr[dc].ToString()); 10 } 11 list.Add(result); 12 } 13 return ToJson(list);//调用Serializer方法 14 }
1 /// <summary> 2 /// 将集合转换为json 3 /// </summary> 4 /// <param name="d"></param> 5 /// <returns></returns> 6 public static string ToJson(object d) 7 { 8 JavaScriptSerializer jss = new JavaScriptSerializer(); 9 return jss.Serialize(d); 10 }
具体 转换实例:
1 #region 获取我的订单 2 public string GetMyOrders(string _StrJson) 3 { 4 StringBuilder sbStr = new StringBuilder(); 5 sbStr.Append("[{"msg":"获取失败,参数错误。","data":"[]","state":"2"}]"); 6 try 7 { 8 CommonJsonModel oModel = new CommonJsonModel(Regex.Replace(_StrJson, @" ", "")); 9 List<CommonJsonModel> lst = oModel.GetCollection(); 10 foreach (CommonJsonModel item in lst) 11 { 12 //[{"_Page":"1","_PageSize":"10","_Type":"1","_Status":"0","_LoginId":"15","_Profession":"390"}] 13 int Page = item.GetValue("_Page") == "" ? 1 : Convert.ToInt32(item.GetValue("_Page")); 14 int PageSize = item.GetValue("_PageSize") == "" ? 10 : Convert.ToInt32(item.GetValue("_PageSize")); 15 int Type = item.GetValue("_Type") == "" ? 0 : Convert.ToInt32(item.GetValue("_Type")); 16 int Status = item.GetValue("_Status") == "" ? 0 : Convert.ToInt32(item.GetValue("_Status")); 17 int LoginId = item.GetValue("_LoginId") == "" ? 0 : Convert.ToInt32(item.GetValue("_LoginId")); 18 string Profession = item.GetValue("_Profession"); 19 StringBuilder sbSqlWhere = new StringBuilder(); 20 sbSqlWhere.Append(" PersonIsdel=0"); 21 if (Status > 0) { sbSqlWhere.AppendFormat(" and State={0}", Status); } 22 if (LoginId > 0) { sbSqlWhere.AppendFormat(" and AddPerson={0}", LoginId); } 23 if (Profession != "") 24 { 25 sbSqlWhere.AppendFormat(" and BusinessId in(select ID from TB_Business where Profession={0})", Profession); 26 } 27 StringBuilder sbFields = new StringBuilder(); 28 sbFields.AppendFormat("*", Type); 29 DataSet ds = bll.GetList("TB_BusinessOrders", sbFields.ToString(), "AddTime", PageSize, Page, false, true, sbSqlWhere.ToString()); 30 if (ds != null && ds.Tables[0].Rows.Count > 0) 31 {
//第一个DataTable 32 DataTable dt = ds.Tables[0];
//新增一列子集 33 dt.Columns.Add("OrdelDetailList"); 34 foreach (DataRow dr in dt.Rows) 35 { 36 dr["OrdelDetailList"] = GetOrderDetailList(dr["ID"].ToString()); 37 } 38 sbStr.Clear();
//整 个DataTable转Json 39 string strTemp = EasyUIJsonHelper.TableToJson(ds.Tables[0]);
//去除多余的括号转义符号 40 strTemp = strTemp.Replace("\", ""); 41 strTemp = strTemp.Replace(""[{", "[{"); 42 strTemp = strTemp.Replace("}]"", "}]");
// 完毕 43 sbStr.Append("[{"msg":"获取成功。","data":" + strTemp + ","state":"0"}]"); 44 } 45 else 46 { 47 sbStr.Clear(); 48 sbStr.Append("[{"msg":"获取成功,无匹配数据。","data":"[]","state":"0"}]"); 49 } 50 } 51 } 52 catch { } 53 return sbStr.ToString(); 54 } 55 56 private string GetOrderDetailList(string orderId) 57 { 58 StringBuilder sbStr = new StringBuilder(); 59 DataSet ds = new ZhouFu.Bll.TB_BusinessOrdersDetail().GetList("OrderId=" + orderId); 60 if (ds != null && ds.Tables[0].Rows.Count > 0) 61 { 62 DataTable dt = ds.Tables[0]; 63 sbStr.Clear(); 64 sbStr.Append(EasyUIJsonHelper.TableToJson(dt)); 65 } 66 return sbStr.ToString(); 67 } 68 #endregion