JsonHelper.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* 作者: 季健国
  2. * 创建时间: 2012/6/9 23:10:11
  3. *
  4. */
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Web.Script.Serialization;
  10. using Newtonsoft.Json;
  11. using System.Xml;
  12. using System.Runtime.Serialization.Json;
  13. using System.IO;
  14. using System.Data;
  15. using System.Collections;
  16. namespace Ant.Service.Common
  17. {
  18. /// <summary>
  19. /// 提供了一个关于json的辅助类
  20. /// </summary>
  21. public class JsonHelper
  22. {
  23. #region DataTable<-->JSON
  24. /// <summary>
  25. /// DataTable转为json
  26. /// </summary>
  27. /// <param name="dt">DataTable</param>
  28. /// <returns>json数据</returns>
  29. public static string DataTableToJson(DataTable dt)
  30. {
  31. List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
  32. foreach (DataRow dr in dt.Rows)
  33. {
  34. Dictionary<string, object> result = new Dictionary<string, object>();
  35. foreach (DataColumn dc in dt.Columns)
  36. {
  37. result.Add(dc.ColumnName, dr[dc]);
  38. }
  39. list.Add(result);
  40. }
  41. return SerializeToJson(list);
  42. }
  43. /// <summary>
  44. /// 序列化对象为Json字符串
  45. /// </summary>
  46. /// <param name="obj">要序列化的对象</param>
  47. /// <param name="recursionLimit">序列化对象的深度,默认为100</param>
  48. /// <returns>Json字符串</returns>
  49. public static string SerializeToJson(object obj, int recursionLimit = 100)
  50. {
  51. try
  52. {
  53. JavaScriptSerializer serialize = new JavaScriptSerializer();
  54. serialize.RecursionLimit = recursionLimit;
  55. return serialize.Serialize(obj);
  56. }
  57. catch { return ""; }
  58. }
  59. /// <summary>
  60. /// json包转DataTable
  61. /// </summary>
  62. /// <param name="jsons"></param>
  63. /// <returns></returns>
  64. public static DataTable JsonToDataTable(string jsons)
  65. {
  66. DataTable dt = new DataTable();
  67. try
  68. {
  69. JavaScriptSerializer serialize = new JavaScriptSerializer();
  70. serialize.MaxJsonLength = Int32.MaxValue;
  71. ArrayList list = serialize.Deserialize<ArrayList>(jsons);
  72. if (list.Count > 0)
  73. {
  74. foreach (Dictionary<string, object> item in list)
  75. {
  76. if (item.Keys.Count == 0)//无值返回空
  77. {
  78. return dt;
  79. }
  80. if (dt.Columns.Count == 0)//初始Columns
  81. {
  82. foreach (string current in item.Keys)
  83. {
  84. dt.Columns.Add(current, item[current].GetType());
  85. }
  86. }
  87. DataRow dr = dt.NewRow();
  88. foreach (string current in item.Keys)
  89. {
  90. dr[current] = item[current];
  91. }
  92. dt.Rows.Add(dr);
  93. }
  94. }
  95. }
  96. catch
  97. {
  98. return dt;
  99. }
  100. return dt;
  101. }
  102. #endregion
  103. #region Method
  104. /// <summary>
  105. /// 类对像转换成json格式
  106. /// </summary>
  107. /// <returns></returns>
  108. public static string ToJson(object t)
  109. {
  110. return JsonConvert.SerializeObject(t, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include });
  111. }
  112. /// <summary>
  113. /// 类对像转换成json格式
  114. /// </summary>
  115. /// <param name="t"></param>
  116. /// <param name="HasNullIgnore">是否忽略NULL值</param>
  117. /// <returns></returns>
  118. public static string ToJson(object t, bool HasNullIgnore)
  119. {
  120. if (HasNullIgnore)
  121. return JsonConvert.SerializeObject(t, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
  122. else
  123. return ToJson(t);
  124. }
  125. /// <summary>
  126. /// json格式转换
  127. /// </summary>
  128. /// <typeparam name="T"></typeparam>
  129. /// <param name="strJson"></param>
  130. /// <returns></returns>
  131. public static T FromJson<T>(string strJson) where T : class
  132. {
  133. if (!strJson.IsNullOrEmpty())
  134. return JsonConvert.DeserializeObject<T>(strJson);
  135. return null;
  136. }
  137. /// <summary>
  138. /// 功能描述:将List转换为Json
  139. /// 创建标识:szj
  140. /// </summary>
  141. /// <param name="a"></param>
  142. /// <returns></returns>
  143. public static string ListToJson(IList<object> a)
  144. {
  145. DataContractJsonSerializer json = new DataContractJsonSerializer(a.GetType());
  146. string szJson = "";
  147. //序列化
  148. using (MemoryStream stream = new MemoryStream())
  149. {
  150. json.WriteObject(stream, a);
  151. szJson = Encoding.UTF8.GetString(stream.ToArray());
  152. }
  153. return szJson;
  154. }
  155. #endregion
  156. #region Property
  157. /// <summary>
  158. /// 数据状态
  159. /// </summary>
  160. public string Status { get; set; }
  161. /// <summary>
  162. /// 提示信息
  163. /// </summary>
  164. public string Msg { get; set; }
  165. /// <summary>
  166. /// 回传URL
  167. /// </summary>
  168. public string ReUrl { get; set; }
  169. /// <summary>
  170. /// 数据包
  171. /// </summary>
  172. public object Data { get; set; }
  173. #endregion
  174. }
  175. }