DataTable转换成json字符串

将DataTable里面的行转换成json字符串方法:

 1         #region DataTable转为json 
 2         /// <summary> 
 3         /// DataTable转为json 
 4         /// </summary> 
 5         /// <param name="dt">DataTable</param> 
 6         /// <returns>json数据</returns> 
 7         public static string DataTableToJson(DataTable dt)
 8         {
 9             List<Dictionary<string, object>> list = (from DataRow dr in dt.Rows select dt.Columns.Cast<DataColumn>().ToDictionary(dc => dc.ColumnName, dc => dr[dc])).ToList();
10             return SerializeToJson(list);
11         } 
12         #endregion
13 
14         #region 序列化对象为Json字符串
15         /// <summary>
16         /// 序列化对象为Json字符串
17         /// </summary>
18         /// <param name="obj">要序列化的对象</param>
19         /// <param name="recursionLimit">序列化对象的深度,默认为100</param>
20         /// <returns>Json字符串</returns>
21         public static string SerializeToJson(object obj, int recursionLimit = 100)
22         {
23             try
24             {
25                 System.Web.Script.Serialization.JavaScriptSerializer serialize = new System.Web.Script.Serialization.JavaScriptSerializer();
26                 serialize.RecursionLimit = recursionLimit;
27                 return serialize.Serialize(obj);
28             }
29             catch { return ""; }
30         } 
31         #endregion
32 
33         #region json包转DataTable
34         /// <summary>
35         /// json包转DataTable
36         /// </summary>
37         /// <param name="jsons"></param>
38         /// <returns></returns>
39         public static DataTable JsonToDataTable(string jsons)
40         {
41             DataTable dt = new DataTable();
42             try
43             {
44                 System.Web.Script.Serialization.JavaScriptSerializer serialize = new System.Web.Script.Serialization.JavaScriptSerializer();
45                 serialize.MaxJsonLength = Int32.MaxValue;
46                 ArrayList list = serialize.Deserialize<ArrayList>(jsons);
47                 if (list.Count > 0)
48                 {
49                     foreach (Dictionary<string, object> item in list)
50                     {
51                         if (item.Keys.Count == 0)//无值返回空
52                         {
53                             return dt;
54                         }
55                         if (dt.Columns.Count == 0)//初始Columns
56                         {
57                             foreach (string current in item.Keys)
58                             {
59                                 dt.Columns.Add(current, item[current].GetType());
60                             }
61                         }
62                         DataRow dr = dt.NewRow();
63                         foreach (string current in item.Keys)
64                         {
65                             dr[current] = item[current];
66                         }
67                         dt.Rows.Add(dr);
68                     }
69                 }
70             }
71             catch
72             {
73                 return dt;
74             }
75             return dt;
76         } 
77         #endregion

 

posted @ 2016-07-25 16:45  赛跑的蜗牛  阅读(437)  评论(0编辑  收藏  举报