纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

287 lines
11 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace ICSSoft.ASKDataProject
  8. {
  9. public class JSON
  10. {
  11. /// <summary>
  12. /// DataSet转Json字符串,"主-子-子"结构
  13. /// 需要建立主子表关系,第一张表为主表,第二张表为子表,第三张表为子件表
  14. /// 会排除关联列
  15. /// </summary>
  16. /// <param name="dataSet"></param>
  17. /// <param name="RelationName">关系名称</param>
  18. /// <returns></returns>
  19. public static string DataSetToJson(DataSet dataSet, string RelationName, string column, string RelationName2, string column2)
  20. {
  21. //if (!dataSet.Tables[0].Columns.Contains(column))
  22. //{
  23. //}
  24. DataRelation dataRelation = new DataRelation(RelationName, dataSet.Tables[0].Columns[column], dataSet.Tables[1].Columns[column]);
  25. DataRelation dataRelation2 = new DataRelation(RelationName2, dataSet.Tables[1].Columns[column2], dataSet.Tables[2].Columns[column2]);
  26. dataSet.Relations.Add(dataRelation);
  27. dataSet.Relations.Add(dataRelation2);
  28. StringBuilder jsonString = new StringBuilder();
  29. //foreach (DataTable table in dataSet.Tables)
  30. //{
  31. DataTable table = dataSet.Tables[0];
  32. DataTable table2 = dataSet.Tables[1];
  33. jsonString.Append("[");
  34. DataRowCollection drc = table.Rows;
  35. for (int i = 0; i < drc.Count; i++)
  36. {
  37. DataRow dataRow = drc[i];
  38. jsonString.Append("{");
  39. for (int j = 0; j < table.Columns.Count; j++)
  40. {
  41. string strKey = table.Columns[j].ColumnName;
  42. if (dataSet.Relations[RelationName].ParentColumns.Select(a => a.Caption).Contains(strKey))
  43. continue;
  44. string strValue = dataRow[j].ToString();
  45. Type type = table.Columns[j].DataType;
  46. jsonString.Append("\"" + strKey + "\":");
  47. strValue = StringFormat(strValue, type);
  48. if (j < table.Columns.Count - 1)
  49. {
  50. jsonString.Append(strValue + ",");
  51. }
  52. else
  53. {
  54. jsonString.Append(strValue);
  55. }
  56. }
  57. jsonString.Append(",\"" + RelationName + "\":");
  58. DataRow[] drs = dataRow.GetChildRows(RelationName);
  59. jsonString.Append("[");
  60. foreach (DataRow dr in drs)
  61. {
  62. DataTable dt = dr.Table;
  63. jsonString.Append("{");
  64. for (int j = 0; j < dt.Columns.Count; j++)
  65. {
  66. string strKey = dt.Columns[j].ColumnName;
  67. if (dataSet.Relations[RelationName].ChildColumns.Select(a => a.Caption).Contains(strKey))
  68. continue;
  69. string strValue = dr[j].ToString();
  70. Type type = dt.Columns[j].DataType;
  71. jsonString.Append("\"" + strKey + "\":");
  72. strValue = StringFormat(strValue, type);
  73. if (j < dt.Columns.Count - 1)
  74. {
  75. jsonString.Append(strValue + ",");
  76. }
  77. else
  78. {
  79. jsonString.Append(strValue);
  80. }
  81. }
  82. ////子件表
  83. jsonString.Append(",\"" + RelationName2 + "\":");
  84. //DataRowCollection drc2 = table2.Rows;
  85. //for (int j = 0; j < drc2.Count; j++)
  86. //{
  87. // DataRow dataRow2 = drc2[j];
  88. DataRow[] drss = dr.GetChildRows(RelationName2);//取不到值
  89. jsonString.Append("[");
  90. foreach (DataRow drSec in drss)
  91. {
  92. DataTable dtSec = drSec.Table;
  93. jsonString.Append("{");
  94. for (int k = 0; k < dtSec.Columns.Count; k++)
  95. {
  96. string strKey = dtSec.Columns[k].ColumnName;
  97. if (dataSet.Relations[RelationName2].ChildColumns.Select(a => a.Caption).Contains(strKey))
  98. continue;
  99. string strValue = drSec[k].ToString();
  100. Type type = dtSec.Columns[k].DataType;
  101. jsonString.Append("\"" + strKey + "\":");
  102. strValue = StringFormat(strValue, type);
  103. if (k < dtSec.Columns.Count - 1)
  104. {
  105. jsonString.Append(strValue + ",");
  106. }
  107. else
  108. {
  109. jsonString.Append(strValue);
  110. }
  111. }
  112. jsonString.Append("},");
  113. }
  114. if (drss.Length > 0)
  115. jsonString.Remove(jsonString.Length - 1, 1);
  116. jsonString.Append("]");//detailss的后方括号
  117. jsonString.Append("},");
  118. }
  119. if (drs.Length > 0)
  120. jsonString.Remove(jsonString.Length - 1, 1);
  121. jsonString.Append("]");
  122. jsonString.Append("},");
  123. }
  124. jsonString.Remove(jsonString.Length - 1, 1);
  125. jsonString.Append("]");
  126. //}
  127. string res = jsonString.ToString();
  128. return res;
  129. }
  130. /// <summary>
  131. /// DataSet转Json字符串,主子结构
  132. /// 需要建立主子表关系,第一张表为主表
  133. /// 会排除关联列
  134. /// </summary>
  135. /// <param name="dataSet"></param>
  136. /// <param name="RelationName">关系名称</param>
  137. /// <returns></returns>
  138. public static string DataSetToJson(DataSet dataSet, string RelationName, string column)
  139. {
  140. //if (!dataSet.Tables[0].Columns.Contains(column))
  141. //{
  142. //}
  143. DataRelation dataRelation = new DataRelation(RelationName, dataSet.Tables[0].Columns[column], dataSet.Tables[1].Columns[column]);
  144. dataSet.Relations.Add(dataRelation);
  145. StringBuilder jsonString = new StringBuilder();
  146. //foreach (DataTable table in dataSet.Tables)
  147. //{
  148. DataTable table = dataSet.Tables[0];
  149. jsonString.Append("[");
  150. DataRowCollection drc = table.Rows;
  151. for (int i = 0; i < drc.Count; i++)
  152. {
  153. DataRow dataRow = drc[i];
  154. jsonString.Append("{");
  155. for (int j = 0; j < table.Columns.Count; j++)
  156. {
  157. string strKey = table.Columns[j].ColumnName;
  158. if (dataSet.Relations[RelationName].ParentColumns.Select(a => a.Caption).Contains(strKey))
  159. continue;
  160. string strValue = dataRow[j].ToString();
  161. Type type = table.Columns[j].DataType;
  162. jsonString.Append("\"" + strKey + "\":");
  163. strValue = StringFormat(strValue, type);
  164. if (j < table.Columns.Count - 1)
  165. {
  166. jsonString.Append(strValue + ",");
  167. }
  168. else
  169. {
  170. jsonString.Append(strValue);
  171. }
  172. }
  173. jsonString.Append(",\"" + RelationName + "\":");
  174. DataRow[] drs = dataRow.GetChildRows(RelationName);
  175. jsonString.Append("[");
  176. foreach (DataRow dr in drs)
  177. {
  178. DataTable dt = dr.Table;
  179. jsonString.Append("{");
  180. for (int j = 0; j < dt.Columns.Count; j++)
  181. {
  182. string strKey = dt.Columns[j].ColumnName;
  183. if (dataSet.Relations[RelationName].ChildColumns.Select(a => a.Caption).Contains(strKey))
  184. continue;
  185. string strValue = dr[j].ToString();
  186. Type type = dt.Columns[j].DataType;
  187. jsonString.Append("\"" + strKey + "\":");
  188. strValue = StringFormat(strValue, type);
  189. if (j < dt.Columns.Count - 1)
  190. {
  191. jsonString.Append(strValue + ",");
  192. }
  193. else
  194. {
  195. jsonString.Append(strValue);
  196. }
  197. }
  198. jsonString.Append("},");
  199. }
  200. if (drs.Length > 0)
  201. jsonString.Remove(jsonString.Length - 1, 1);
  202. jsonString.Append("]");
  203. jsonString.Append("},");
  204. }
  205. jsonString.Remove(jsonString.Length - 1, 1);
  206. jsonString.Append("]");
  207. //}
  208. string res = jsonString.ToString();
  209. return res;
  210. }
  211. /// <summary>
  212. /// 格式化字符型、日期型、布尔型
  213. /// </summary>
  214. /// <param name="str"></param>
  215. /// <param name="type"></param>
  216. /// <returns></returns>
  217. private static string StringFormat(string str, Type type)
  218. {
  219. if (type == typeof(string))
  220. {
  221. str = String2Json(str);
  222. str = "\"" + str + "\"";
  223. }
  224. else if (type == typeof(DateTime))
  225. {
  226. str = "\"" + str + "\"";
  227. }
  228. else if (type == typeof(bool))
  229. {
  230. str = str.ToLower();
  231. }
  232. else if (type != typeof(string) && string.IsNullOrEmpty(str))
  233. {
  234. str = "\"" + str + "\"";
  235. }
  236. return str;
  237. }
  238. /// <summary>
  239. /// 过滤特殊字符
  240. /// </summary>
  241. /// <param name="s">字符串</param>
  242. /// <returns>json字符串</returns>
  243. private static string String2Json(String s)
  244. {
  245. StringBuilder sb = new StringBuilder();
  246. for (int i = 0; i < s.Length; i++)
  247. {
  248. char c = s.ToCharArray()[i];
  249. switch (c)
  250. {
  251. case '\"':
  252. sb.Append("\\\""); break;
  253. case '\\':
  254. sb.Append("\\\\"); break;
  255. case '/':
  256. sb.Append("\\/"); break;
  257. case '\b':
  258. sb.Append("\\b"); break;
  259. case '\f':
  260. sb.Append("\\f"); break;
  261. case '\n':
  262. sb.Append("\\n"); break;
  263. case '\r':
  264. sb.Append("\\r"); break;
  265. case '\t':
  266. sb.Append("\\t"); break;
  267. default:
  268. sb.Append(c); break;
  269. }
  270. }
  271. return sb.ToString();
  272. }
  273. }
  274. }