JsonHelper.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Collections;
  5. using Ant.ORM.Table;
  6. using System.Data;
  7. namespace Ant.ORM
  8. {
  9. public class JsonHelper
  10. {
  11. /// <summary>
  12. /// 是否成功
  13. /// </summary>
  14. public bool Success
  15. {
  16. get
  17. {
  18. return count > 0;
  19. }
  20. }
  21. private string errorMsg = "";
  22. /// <summary>
  23. /// 错误提示信息
  24. /// </summary>
  25. public string ErrorMsg
  26. {
  27. get
  28. {
  29. return errorMsg;
  30. }
  31. set
  32. {
  33. errorMsg = value;
  34. }
  35. }
  36. private int count = 0;
  37. /// <summary>
  38. /// 总数
  39. /// </summary>
  40. public int Count
  41. {
  42. get
  43. {
  44. return count;
  45. }
  46. set
  47. {
  48. count = value;
  49. }
  50. }
  51. private List<string> arrData = new List<string>();
  52. #region 对象与对象之间分割符
  53. public void addItemOk()
  54. {
  55. arrData.Add("<br>");
  56. }
  57. #endregion
  58. #region 在数组里添加key,value
  59. public void addItem(string name, string value)
  60. {
  61. arrData.Add("\"" + name + "\":" + "\"" + value + "\"");
  62. }
  63. #endregion
  64. #region 返回组装好的json字符串
  65. public override string ToString()
  66. {
  67. StringBuilder sb = new StringBuilder();
  68. sb.Append("{");
  69. sb.Append("\"count\":\"" + count + "\",");
  70. sb.Append("\"error\":\"" + errorMsg + "\",");
  71. sb.Append("\"success\":\"" + (Success ? "true" : "") + "\",");
  72. sb.Append("\"data\":[");
  73. int index = 0;
  74. sb.Append("{");
  75. if (arrData.Count <= 0)
  76. {
  77. sb.Append("}]");
  78. }
  79. else
  80. {
  81. foreach (string val in arrData)
  82. {
  83. index++;
  84. if (val != "<br>")
  85. {
  86. sb.Append(val + ",");
  87. }
  88. else
  89. {
  90. sb = sb.Replace(",", "", sb.Length - 1, 1);
  91. sb.Append("},");
  92. if (index < arrData.Count)
  93. {
  94. sb.Append("{");
  95. }
  96. }
  97. }
  98. sb = sb.Replace(",", "", sb.Length - 1, 1);
  99. sb.Append("]");
  100. }
  101. sb.Append("}");
  102. return sb.ToString();
  103. }
  104. #endregion
  105. #region 为DataTable增加处理
  106. /// <summary>
  107. /// 从数据表中取数据填充,最终可输出json字符串
  108. /// </summary>
  109. public void Fill(MDataTable table)
  110. {
  111. if (table == null)
  112. {
  113. ErrorMsg = "table object is null";
  114. return;
  115. }
  116. Count = table.Rows.Count;
  117. for (int i = 0; i < table.Rows.Count; i++)
  118. {
  119. for (int j = 0; j < table.Columns.Count; j++)
  120. {
  121. addItem(table.Columns[j].ColumnName, Convert.ToString(table.Rows[i][j].Value));
  122. }
  123. addItemOk();
  124. }
  125. }
  126. /// <summary>
  127. /// 从Json字符串中反加载成数据表,默认字段结构都为string类型
  128. /// </summary>
  129. public MDataTable Load(string json)
  130. {
  131. MDataTable table = new MDataTable("loadFromJson");
  132. if (!string.IsNullOrEmpty(json) && json.StartsWith("{") && json.IndexOf(',') > -1 && json.EndsWith("}"))
  133. {
  134. List<string> items = new List<string>();
  135. if (json.Contains(":[{"))//多条数据
  136. {
  137. int start = json.IndexOf(":[{") + 2;
  138. json = json.Substring(start, json.LastIndexOf("}]") - start);
  139. string splitKey = "},{";
  140. start = json.IndexOf(splitKey);
  141. while (start > -1)
  142. {
  143. items.Add(json.Substring(0, start + 1));
  144. json = json.Remove(0, start + 2);
  145. start = json.IndexOf(splitKey);
  146. }
  147. }
  148. items.Add(json);
  149. if (items.Count > 0)
  150. {
  151. Dictionary<string, string> keyValueDic = Split(items[0]);
  152. if (keyValueDic != null && keyValueDic.Count > 0)
  153. {
  154. foreach (KeyValuePair<string, string> item in keyValueDic)
  155. {
  156. table.Columns.Add(item.Key, SqlDbType.NVarChar);
  157. }
  158. }
  159. foreach (string jsonItem in items)
  160. {
  161. keyValueDic = Split(jsonItem);
  162. if (keyValueDic != null && keyValueDic.Count > 0)
  163. {
  164. MDataRow row = table.NewRow();
  165. foreach (KeyValuePair<string, string> item in keyValueDic)
  166. {
  167. row.Set(item.Key, item.Value);
  168. }
  169. table.Rows.Add(row);
  170. }
  171. }
  172. }
  173. }
  174. return table;
  175. }
  176. public static Dictionary<string, string> Split(string json)
  177. {
  178. if (string.IsNullOrEmpty(json) || !json.StartsWith("{") || !json.EndsWith("}"))
  179. {
  180. return null;
  181. }
  182. json = json.TrimStart('{').TrimEnd('}');
  183. List<string> items = new List<string>();
  184. string splitKey = "\",\"";
  185. int index = json.IndexOf(splitKey);
  186. if (index == -1)
  187. {
  188. splitKey = "','";
  189. index = json.IndexOf(splitKey);
  190. if (index == -1)
  191. {
  192. splitKey = ",";
  193. index = json.IndexOf(splitKey);
  194. }
  195. }
  196. int startIndex = 0;
  197. string item = string.Empty;
  198. while (index > -1)
  199. {
  200. item = json.Substring(0, index + 1);
  201. if (item.Contains(":"))
  202. {
  203. items.Add(item);
  204. json = json.Remove(0, index + 2);
  205. startIndex = 0;
  206. }
  207. else
  208. {
  209. startIndex = index + 2;
  210. }
  211. index = json.IndexOf(splitKey, startIndex);
  212. }
  213. items.Add(json);
  214. Dictionary<string, string> keyValueDic = null;
  215. if (items.Count > 0)
  216. {
  217. keyValueDic = new Dictionary<string, string>();
  218. foreach (string keyValue in items)
  219. {
  220. index = keyValue.IndexOf(':');
  221. if (index > -1)
  222. {
  223. keyValueDic.Add(keyValue.Substring(0, index).Trim('\'', '\"'), keyValue.Substring(index + 1).Trim('\'', '\"'));
  224. }
  225. }
  226. }
  227. return keyValueDic;
  228. }
  229. #endregion
  230. }
  231. }