c#常用的Datable转换为json,以及json转换为DataTable操作方法
2020-12-13 01:27
标签:des class java tar ext http c#常用的Datable转换为json,以及json转换为DataTable操作方法,搜素材,soscw.com c#常用的Datable转换为json,以及json转换为DataTable操作方法 标签:des class java tar ext http 原文地址:http://www.cnblogs.com/kexb/p/3708498.html
[1].[代码] [C#]Datatable和json互相转换操作 跳至 [1]
view
source
print?
#region DataTable
转换为Json字符串实例方法
///
///
GetClassTypeJosn 的摘要说明
///
public
class GetClassTypeJosn : IHttpHandler
{
///
/// 文件名:DataTable 和Json 字符串互转
///
版权所有:Copyright (C) Create Family Wealth liangjw
/// 创建标示:2013-08-03
///
//用法说明实例
public void ProcessRequest(HttpContext
context)
{
context.Response.ContentType =
"application/json";
context.Response.Charset = "utf-8";
HttpRequest req =
context.Request;
string method = req["method"].ToStr().ToLower();
//获取合同明细列表 DataTable
转换为Json字符串
if (method
== "txtdate")
{
string
json =
"";
BO.MakeContractMx bll = new MakeContractMx();
DataSet
ds =
bll.GetDataTable();
if (ds.Tables.Count > 0)
{
json =ToJson(ds.Tables[0]);
}
context.Response.Write(json);
return;
}
}
public bool
IsReusable
{
get
{
return
false;
}
}
}
#endregion
#region Json字符串转换为DataTable 实例方法
public DataTable
JsonToDataTable(json)
{
DataTable dt=
ToDataTable(json);
return
dt;
}
#endregion
#region DataTable 转换为Json 字符串
///
/// DataTable 对象 转换为Json 字符串
///
///
///
public static
string ToJson(this DataTable dt)
{
JavaScriptSerializer javaScriptSerializer = new
JavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxValue;
//取得最大数值
ArrayList
arrayList = new ArrayList();
foreach (DataRow dataRow in
dt.Rows)
{
Dictionary
foreach
(DataColumn dataColumn in dt.Columns)
{
dictionary.Add(dataColumn.ColumnName,
dataRow[dataColumn.ColumnName].ToStr());
}
arrayList.Add(dictionary); //ArrayList集合中添加键值
}
return
javaScriptSerializer.Serialize(arrayList); //返回一个json字符串
}
#endregion
#region Json 字符串 转换为 DataTable数据集合
///
/// Json 字符串 转换为
DataTable数据集合
///
///
///
public
static DataTable ToDataTable(this string json)
{
DataTable dataTable = new
DataTable(); //实例化
DataTable
result;
try
{
JavaScriptSerializer javaScriptSerializer = new
JavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
ArrayList arrayList =
javaScriptSerializer.Deserialize(json);
if
(arrayList.Count > 0)
{
foreach (Dictionary
{
if (dictionary.Keys.Count
{
result = dataTable;
return result;
}
if (dataTable.Columns.Count == 0)
{
foreach (string current in dictionary.Keys)
{
dataTable.Columns.Add(current, dictionary[current].GetType());
}
}
DataRow dataRow = dataTable.NewRow();
foreach (string current in dictionary.Keys)
{
dataRow[current] = dictionary[current];
}
dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
}
}
}
catch
{
}
result =
dataTable;
return result;
}
#endregion
#region 转换为string字符串类型
///
/// 转换为string字符串类型
///
/// 获取需要转换的值
///
需要格式化的位数
///
public static string ToStr(this object s, string format
= "")
{
string result =
"";
try
{
if
(format == "")
{
result = s.ToString();
}
else
{
result = string.Format("{0:" + format + "}", s);
}
}
catch
{
}
return
result;
}
#endregion
文章标题:c#常用的Datable转换为json,以及json转换为DataTable操作方法
文章链接:http://soscw.com/essay/23814.html