首页 给出 DataTable 转Json 的方法:
public static string TableToJson(DataTable dt)
{
List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
foreach (DataRow dr in dt.Rows)//每一行信息,新建一个Dictionary<string,object>,将该行的每列信息加入到字典
{
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 ToJson(list);//调用Serializer方法
}
/// <summary>
/// 将集合转换为json
/// </summary>
/// <param name="d"></param>
/// <returns></returns>
public static string ToJson(object d)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
return jss.Serialize(d);
}
具体 转换实例:
#region 获取我的订单
public string GetMyOrders(string _StrJson)
{
StringBuilder sbStr = new StringBuilder();
sbStr.Append("[{\"msg\":\"获取失败,参数错误。\",\"data\":\"[]\",\"state\":\"2\"}]");
try
{
CommonJsonModel oModel = new CommonJsonModel(Regex.Replace(_StrJson, @"\r\n", ""));
List<CommonJsonModel> lst = oModel.GetCollection();
foreach (CommonJsonModel item in lst)
{
//[{"_Page":"1","_PageSize":"10","_Type":"1","_Status":"0","_LoginId":"15","_Profession":"390"}]
int Page = item.GetValue("_Page") == "" ? : Convert.ToInt32(item.GetValue("_Page"));
int PageSize = item.GetValue("_PageSize") == "" ? : Convert.ToInt32(item.GetValue("_PageSize"));
int Type = item.GetValue("_Type") == "" ? : Convert.ToInt32(item.GetValue("_Type"));
int Status = item.GetValue("_Status") == "" ? : Convert.ToInt32(item.GetValue("_Status"));
int LoginId = item.GetValue("_LoginId") == "" ? : Convert.ToInt32(item.GetValue("_LoginId"));
string Profession = item.GetValue("_Profession");
StringBuilder sbSqlWhere = new StringBuilder();
sbSqlWhere.Append(" PersonIsdel=0");
if (Status > ) { sbSqlWhere.AppendFormat(" and State={0}", Status); }
if (LoginId > ) { sbSqlWhere.AppendFormat(" and AddPerson={0}", LoginId); }
if (Profession != "")
{
sbSqlWhere.AppendFormat(" and BusinessId in(select ID from TB_Business where Profession={0})", Profession);
}
StringBuilder sbFields = new StringBuilder();
sbFields.AppendFormat("*", Type);
DataSet ds = bll.GetList("TB_BusinessOrders", sbFields.ToString(), "AddTime", PageSize, Page, false, true, sbSqlWhere.ToString());
if (ds != null && ds.Tables[].Rows.Count > )
{
//第一个DataTable
DataTable dt = ds.Tables[0];
//新增一列子集
dt.Columns.Add("OrdelDetailList");
foreach (DataRow dr in dt.Rows)
35 {
36 dr["OrdelDetailList"] = GetOrderDetailList(dr["ID"].ToString());
37 }
sbStr.Clear();
//整 个DataTable转Json
string strTemp = EasyUIJsonHelper.TableToJson(ds.Tables[0]);
//去除多余的括号转义符号
40 strTemp = strTemp.Replace("\\", "");
41 strTemp = strTemp.Replace("\"[{", "[{");
42 strTemp = strTemp.Replace("}]\"", "}]");
// 完毕
sbStr.Append("[{\"msg\":\"获取成功。\",\"data\":" + strTemp + ",\"state\":\"0\"}]");
}
else
{
sbStr.Clear();
sbStr.Append("[{\"msg\":\"获取成功,无匹配数据。\",\"data\":\"[]\",\"state\":\"0\"}]");
}
}
}
catch { }
return sbStr.ToString();
} private string GetOrderDetailList(string orderId)
{
StringBuilder sbStr = new StringBuilder();
DataSet ds = new ZhouFu.Bll.TB_BusinessOrdersDetail().GetList("OrderId=" + orderId);
if (ds != null && ds.Tables[].Rows.Count > )
{
DataTable dt = ds.Tables[];
sbStr.Clear();
sbStr.Append(EasyUIJsonHelper.TableToJson(dt));
}
return sbStr.ToString();
}
#endregion