为方便大家参考,我现将这代码复制出来为以下内容:
// 2020-10-25暂取消,
#region DataTable 转换为Json 字符串
/// <summary>
/// DataTable 对象 转换为Json 字符串
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static string Hua_Tojson_ToJson(DataTable dt)
{
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
ArrayList arrayList = new ArrayList();
foreach (DataRow dataRow in dt.Rows)
{
Dictionary<string, object> dictionary = new Dictionary<string, object>(); //实例化一个参数集合
foreach (DataColumn dataColumn in dt.Columns)
{
dictionary.Add(dataColumn.ColumnName, dataRow[dataColumn.ColumnName].ToString());
}
arrayList.Add(dictionary); //ArrayList集合中添加键值
}
return javaScriptSerializer.Serialize(arrayList); //返回一个json字符串
}
#endregion
|