[代码] [C#]Datatable和json互相转换操作
#region DataTable 转换为Json字符串实例方法
/// <summary>
/// GetClassTypeJosn 的摘要说明
/// </summary>
publicclassGetClassTypeJosn : IHttpHandler
{
/// <summary>
/// 文件名:DataTable 和Json 字符串互转
/// 版权所有:Copyright (C) Create Family Wealth liangjw
/// 创建标示:2013-08-03
/// </summary>
//用法说明实例
publicvoidProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
context.Response.Charset = "utf-8";
HttpRequest req = context.Request;
stringmethod = req["method"].ToStr().ToLower();
//获取合同明细列表 DataTable 转换为Json字符串
if(method == "txtdate")
{
stringjson = "";
BO.MakeContractMx bll = newMakeContractMx();
DataSet ds = bll.GetDataTable();
if(ds.Tables.Count > 0)
{
json =ToJson(ds.Tables[0]);
}
context.Response.Write(json);
return;
}
}
publicboolIsReusable
{
get
{
returnfalse;
}
}
}
#endregion
#region Json字符串转换为DataTable 实例方法
publicDataTable JsonToDataTable(json)
{
DataTable dt= ToDataTable(json);
returndt;
}
#endregion
#region DataTable 转换为Json 字符串
/// <summary>
/// DataTable 对象 转换为Json 字符串
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
publicstaticstringToJson(thisDataTable dt)
{
JavaScriptSerializer javaScriptSerializer = newJavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
ArrayList arrayList = newArrayList();
foreach(DataRow dataRow indt.Rows)
{
Dictionary<string, object> dictionary = newDictionary<string, object>(); //实例化一个参数集合
foreach(DataColumn dataColumn indt.Columns)
{
dictionary.Add(dataColumn.ColumnName, dataRow[dataColumn.ColumnName].ToStr());
}
arrayList.Add(dictionary); //ArrayList集合中添加键值
}
returnjavaScriptSerializer.Serialize(arrayList); //返回一个json字符串
}
#endregion
#region Json 字符串 转换为 DataTable数据集合
/// <summary>
/// Json 字符串 转换为 DataTable数据集合
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
publicstaticDataTable ToDataTable(thisstringjson)
{
DataTable dataTable = newDataTable(); //实例化
DataTable result;
try
{
JavaScriptSerializer javaScriptSerializer = newJavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json);
if(arrayList.Count > 0)
{
foreach(Dictionary<string, object> dictionary inarrayList)
{
if(dictionary.Keys.Count<string>() == 0)
{
result = dataTable;
returnresult;
}
if(dataTable.Columns.Count == 0)
{
foreach(stringcurrent indictionary.Keys)
{
dataTable.Columns.Add(current, dictionary[current].GetType());
}
}
DataRow dataRow = dataTable.NewRow();
foreach(stringcurrent indictionary.Keys)
{
dataRow[current] = dictionary[current];
}
dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
}
}
}
catch
{
}
result = dataTable;
returnresult;
}
#endregion
#region 转换为string字符串类型
/// <summary>
/// 转换为string字符串类型
/// </summary>
/// <param name="s">获取需要转换的值</param>
/// <param name="format">需要格式化的位数</param>
/// <returns>返回一个新的字符串</returns>
publicstaticstringToStr(thisobjects, stringformat = "")
{
stringresult = "";
try
{
if(format == "")
{
result = s.ToString();
}
else
{
result = string.Format("{0:"+ format + "}", s);
}
}
catch
{
}
returnresult;
}
#endregion
[代码] [C#]Datatable和json互相转换操作
#region DataTable 转换为Json字符串实例方法
/// <summary>
/// GetClassTypeJosn 的摘要说明
/// </summary>
publicclassGetClassTypeJosn : IHttpHandler
{
/// <summary>
/// 文件名:DataTable 和Json 字符串互转
/// 版权所有:Copyright (C) Create Family Wealth liangjw
/// 创建标示:2013-08-03
/// </summary>
//用法说明实例
publicvoidProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
context.Response.Charset = "utf-8";
HttpRequest req = context.Request;
stringmethod = req["method"].ToStr().ToLower();
//获取合同明细列表 DataTable 转换为Json字符串
if(method == "txtdate")
{
stringjson = "";
BO.MakeContractMx bll = newMakeContractMx();
DataSet ds = bll.GetDataTable();
if(ds.Tables.Count > 0)
{
json =ToJson(ds.Tables[0]);
}
context.Response.Write(json);
return;
}
}
publicboolIsReusable
{
get
{
returnfalse;
}
}
}
#endregion
#region Json字符串转换为DataTable 实例方法
publicDataTable JsonToDataTable(json)
{
DataTable dt= ToDataTable(json);
returndt;
}
#endregion
#region DataTable 转换为Json 字符串
/// <summary>
/// DataTable 对象 转换为Json 字符串
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
publicstaticstringToJson(thisDataTable dt)
{
JavaScriptSerializer javaScriptSerializer = newJavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
ArrayList arrayList = newArrayList();
foreach(DataRow dataRow indt.Rows)
{
Dictionary<string, object> dictionary = newDictionary<string, object>(); //实例化一个参数集合
foreach(DataColumn dataColumn indt.Columns)
{
dictionary.Add(dataColumn.ColumnName, dataRow[dataColumn.ColumnName].ToStr());
}
arrayList.Add(dictionary); //ArrayList集合中添加键值
}
returnjavaScriptSerializer.Serialize(arrayList); //返回一个json字符串
}
#endregion
#region Json 字符串 转换为 DataTable数据集合
/// <summary>
/// Json 字符串 转换为 DataTable数据集合
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
publicstaticDataTable ToDataTable(thisstringjson)
{
DataTable dataTable = newDataTable(); //实例化
DataTable result;
try
{
JavaScriptSerializer javaScriptSerializer = newJavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json);
if(arrayList.Count > 0)
{
foreach(Dictionary<string, object> dictionary inarrayList)
{
if(dictionary.Keys.Count<string>() == 0)
{
result = dataTable;
returnresult;
}
if(dataTable.Columns.Count == 0)
{
foreach(stringcurrent indictionary.Keys)
{
dataTable.Columns.Add(current, dictionary[current].GetType());
}
}
DataRow dataRow = dataTable.NewRow();
foreach(stringcurrent indictionary.Keys)
{
dataRow[current] = dictionary[current];
}
dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
}
}
}
catch
{
}
result = dataTable;
returnresult;
}
#endregion
#region 转换为string字符串类型
/// <summary>
/// 转换为string字符串类型
/// </summary>
/// <param name="s">获取需要转换的值</param>
/// <param name="format">需要格式化的位数</param>
/// <returns>返回一个新的字符串</returns>
publicstaticstringToStr(thisobjects, stringformat = "")
{
stringresult = "";
try
{
if(format == "")
{
result = s.ToString();
}
else
{
result = string.Format("{0:"+ format + "}", s);
}
}
catch
{
}
returnresult;
}
#endregion