c#常用的Datable转换为json,以及json转换为DataTable操作方法

时间:2023-03-08 19:15:18
  1. #region  DataTable 转换为Json字符串实例方法
  2. /// <summary>
  3. /// GetClassTypeJosn 的摘要说明
  4. /// </summary>
  5. public class GetClassTypeJosn : IHttpHandler
  6. {
  7. /// <summary>
  8. /// 文件名:DataTable 和Json 字符串互转
  9. /// 版权所有:Copyright (C) Create Family Wealth liangjw
  10. /// 创建标示:2013-08-03
  11. /// </summary>
  12. //用法说明实例
  13. public void ProcessRequest(HttpContext context)
  14. {
  15. context.Response.ContentType = "application/json";
  16. context.Response.Charset = "utf-8";
  17. HttpRequest req = context.Request;
  18. string method = req["method"].ToStr().ToLower();
  19. //获取合同明细列表  DataTable 转换为Json字符串
  20. if (method == "txtdate")
  21. {
  22. string json = "";
  23. BO.MakeContractMx bll = new MakeContractMx();
  24. DataSet ds = bll.GetDataTable();
  25. if (ds.Tables.Count > 0)
  26. {
  27. json =ToJson(ds.Tables[0]);
  28. }
  29. context.Response.Write(json);
  30. return;
  31. }
  32. }
  33. public bool IsReusable
  34. {
  35. get
  36. {
  37. return false;
  38. }
  39. }
  40. }
  41. #endregion
  42. #region Json字符串转换为DataTable 实例方法
  43. public DataTable JsonToDataTable(json)
  44. {
  45. DataTable  dt= ToDataTable(json);
  46. return dt;
  47. }
  48. #endregion
  49. #region DataTable 转换为Json 字符串
  50. /// <summary>
  51. /// DataTable 对象 转换为Json 字符串
  52. /// </summary>
  53. /// <param name="dt"></param>
  54. /// <returns></returns>
  55. public static string ToJson(this DataTable dt)
  56. {
  57. JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
  58. javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
  59. ArrayList arrayList = new ArrayList();
  60. foreach (DataRow dataRow in dt.Rows)
  61. {
  62. Dictionary<string, object> dictionary = new Dictionary<string, object>();  //实例化一个参数集合
  63. foreach (DataColumn dataColumn in dt.Columns)
  64. {
  65. dictionary.Add(dataColumn.ColumnName, dataRow[dataColumn.ColumnName].ToStr());
  66. }
  67. arrayList.Add(dictionary); //ArrayList集合中添加键值
  68. }
  69. return javaScriptSerializer.Serialize(arrayList);  //返回一个json字符串
  70. }
  71. #endregion
  72. #region Json 字符串 转换为 DataTable数据集合
  73. /// <summary>
  74. /// Json 字符串 转换为 DataTable数据集合
  75. /// </summary>
  76. /// <param name="json"></param>
  77. /// <returns></returns>
  78. public static DataTable ToDataTable(this string json)
  79. {
  80. DataTable dataTable = new DataTable();  //实例化
  81. DataTable result;
  82. try
  83. {
  84. JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
  85. javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
  86. ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json);
  87. if (arrayList.Count > 0)
  88. {
  89. foreach (Dictionary<string, object> dictionary in arrayList)
  90. {
  91. if (dictionary.Keys.Count<string>() == 0)
  92. {
  93. result = dataTable;
  94. return result;
  95. }
  96. if (dataTable.Columns.Count == 0)
  97. {
  98. foreach (string current in dictionary.Keys)
  99. {
  100. dataTable.Columns.Add(current, dictionary[current].GetType());
  101. }
  102. }
  103. DataRow dataRow = dataTable.NewRow();
  104. foreach (string current in dictionary.Keys)
  105. {
  106. dataRow[current] = dictionary[current];
  107. }
  108. dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
  109. }
  110. }
  111. }
  112. catch
  113. {
  114. }
  115. result = dataTable;
  116. return result;
  117. }
  118. #endregion
  119. #region 转换为string字符串类型
  120. /// <summary>
  121. ///  转换为string字符串类型
  122. /// </summary>
  123. /// <param name="s">获取需要转换的值</param>
  124. /// <param name="format">需要格式化的位数</param>
  125. /// <returns>返回一个新的字符串</returns>
  126. public static string ToStr(this object s, string format = "")
  127. {
  128. string result = "";
  129. try
  130. {
  131. if (format == "")
  132. {
  133. result = s.ToString();
  134. }
  135. else
  136. {
  137. result = string.Format("{0:" + format + "}", s);
  138. }
  139. }
  140. catch
  141. {
  142. }
  143. return result;
  144. }
  145. #endregion

/*==============================================================================
*
* Filename: DatatableToJson.cs
* Description: 主要包含两个方法:
   1. 获取的DataTable 对象 转换为Json 字符串
   2. Json 字符串 转换为 DataTable数据集合