JsonHelper.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;
  6. using System.Web.Script.Serialization;
  7. using System.IO;
  8. using System.Runtime.Serialization;
  9. using System.Reflection;
  10. using System.Text.RegularExpressions;
  11. namespace EMIS.Utility
  12. {
  13. public class JsonHelper
  14. {
  15. //对应JSON的singleInfo成员
  16. public string singleInfo = string.Empty;
  17. protected string _error = string.Empty;
  18. protected bool _success = true;
  19. protected long _totalCount = 0;
  20. protected System.Collections.ArrayList arrData = new ArrayList();
  21. protected System.Collections.ArrayList arrDataItem = new ArrayList();
  22. public JsonHelper()
  23. {
  24. }
  25. public static string ToJSON(object obj)
  26. {
  27. JavaScriptSerializer serializer = new JavaScriptSerializer();
  28. var json = serializer.Serialize(obj);
  29. string pattern = @"\\/Date\((\d+)\)\\/";
  30. MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertJsonDateToDateString);
  31. Regex reg = new Regex(pattern);
  32. return reg.Replace(json, matchEvaluator);
  33. }
  34. /// <summary>
  35. /// 将Json序列化的时间由/Date(1304931520336+0800)/转为字符串
  36. /// </summary>
  37. private static string ConvertJsonDateToDateString(System.Text.RegularExpressions.Match m)
  38. {
  39. string result = string.Empty;
  40. DateTime dt = new DateTime(1970, 1, 1);
  41. dt = dt.AddMilliseconds(long.Parse(m.Groups[1].Value));
  42. dt = dt.ToLocalTime();
  43. result = dt.ToString("yyyy-MM-dd HH:mm:ss");
  44. return result;
  45. }
  46. public static string ToJSON(object obj, int recursionDepth)
  47. {
  48. JavaScriptSerializer serializer = new JavaScriptSerializer();
  49. serializer.RecursionLimit = recursionDepth;
  50. return serializer.Serialize(obj);
  51. }
  52. public static T Deserialize<T>(object obj)
  53. {
  54. JavaScriptSerializer serializer = new JavaScriptSerializer();
  55. return serializer.Deserialize<T>(obj.ToString());
  56. }
  57. public static string ToJsonByValueType(object obj)
  58. {
  59. ArrayList items = new ArrayList();
  60. PropertyInfo[] properties = obj.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
  61. foreach (PropertyInfo p in properties)
  62. {
  63. if (p.PropertyType.IsValueType || p.PropertyType.Name.StartsWith("String"))
  64. {
  65. var value = p.GetValue(obj, null);
  66. if (value != null)
  67. {
  68. if (p.PropertyType.Name.StartsWith("String"))
  69. value = System.Web.HttpContext.Current.Server.UrlEncode(value.ToString());
  70. items.Add(string.Format("{0}:\"{1}\"", p.Name, value));
  71. }
  72. }
  73. }
  74. return string.Format("{{{0}}}", string.Join(",", items.ToArray()));
  75. }
  76. //对应于JSON的success成员
  77. public bool success
  78. {
  79. get
  80. {
  81. return _success;
  82. }
  83. set
  84. {
  85. //如设置为true则清空error
  86. if (success) _error = string.Empty;
  87. _success = value;
  88. }
  89. }
  90. //对应于JSON的error成员
  91. public string error
  92. {
  93. get
  94. {
  95. return _error;
  96. }
  97. set
  98. {
  99. //如设置error,则自动设置success为false
  100. if (value != "") _success = false;
  101. _error = value;
  102. }
  103. }
  104. public long totlalCount
  105. {
  106. get { return _totalCount; }
  107. set { _totalCount = value; }
  108. }
  109. //重置,每次新生成一个json对象时必须执行该方法
  110. public void Reset()
  111. {
  112. _success = true;
  113. _error = string.Empty;
  114. singleInfo = string.Empty;
  115. arrData.Clear();
  116. arrDataItem.Clear();
  117. }
  118. public void AddItem(string name, string value)
  119. {
  120. arrData.Add(name + ":\"" + value + "\"");
  121. }
  122. public void AddItem(string name)
  123. {
  124. arrData.Add(name);
  125. }
  126. public void AddItem2(string name, string value)
  127. {
  128. arrData.Add(name + ":" + value);
  129. }
  130. public void ItemOk()
  131. {
  132. arrData.Add("<BR>");
  133. }
  134. public void AddItemChildren(string name, string value)
  135. {
  136. arrData.Add(name + ":'" + value + "'");
  137. }
  138. //序列化JSON对象,得到返回的JSON代码
  139. public override string ToString()
  140. {
  141. StringBuilder sb = new StringBuilder();
  142. //sb.Append("{");
  143. //sb.Append("totalCount:" + totlalCount.ToString() + ",");
  144. //sb.Append("success:" + _success.ToString().ToLower() + ",");
  145. //sb.Append("error:\"" + _error.Replace("\"", "\\\"") + "\",");
  146. //sb.Append("singleInfo:\"" + singleInfo.Replace("\"", "\\\"") + "\",");
  147. // sb.Append("data:[");
  148. sb.Append("[");
  149. int index = 0;
  150. sb.Append("{");
  151. if (arrData.Count <= 0)
  152. {
  153. sb.Append("}]");
  154. }
  155. else
  156. {
  157. foreach (string val in arrData)
  158. {
  159. index++;
  160. if (val != "<BR>")
  161. {
  162. sb.Append(val + ",");
  163. }
  164. else
  165. {
  166. sb = sb.Replace(",", "", sb.Length - 1, 1);
  167. sb.Append("},");
  168. if (index < arrData.Count)
  169. {
  170. sb.Append("{");
  171. }
  172. }
  173. }
  174. sb = sb.Replace(",", "", sb.Length - 1, 1);
  175. sb.Append("]");
  176. }
  177. //sb.Append("}");
  178. return sb.ToString();
  179. }
  180. //序列化JSON对象,得到返回的JSON代码 //返回子頁的JSON數據
  181. public string ChildrenToString(string strtmp)
  182. {
  183. return "children:" + strtmp;
  184. }
  185. }
  186. }