[C#]Datatable和json互相转换操作

  1 #region  DataTable 转换为Json字符串实例方法
  2 /// <summary>
  3 /// GetClassTypeJosn 的摘要说明
  4 /// </summary>
  5 public class GetClassTypeJosn : IHttpHandler
  6 {
  7     /// <summary>
  8     /// 文件名:DataTable 和Json 字符串互转
  9     /// 版权所有:Copyright (C) Create Family Wealth liangjw
 10     /// 创建标示:2013-08-03
 11     /// </summary> 
 12     //用法说明实例
 13      public void ProcessRequest(HttpContext context)
 14     {
 15         context.Response.ContentType = "application/json";
 16         context.Response.Charset = "utf-8";
 17         HttpRequest req = context.Request;
 18         string method = req["method"].ToStr().ToLower();
 19  
 20  
 21        //获取合同明细列表  DataTable 转换为Json字符串
 22         if (method == "txtdate")
 23         {
 24             string json = "";
 25             BO.MakeContractMx bll = new MakeContractMx();
 26             DataSet ds = bll.GetDataTable();
 27             if (ds.Tables.Count > 0)
 28             {
 29                 json =ToJson(ds.Tables[0]);
 30             }
 31             context.Response.Write(json);
 32             return;
 33         }
 34  
 35     }
 36  
 37    public bool IsReusable
 38     {
 39         get
 40         {
 41             return false;
 42         }
 43     }
 44 }
 45  
 46    #endregion
 47  
 48     #region Json字符串转换为DataTable 实例方法
 49  
 50     public DataTable JsonToDataTable(json)
 51     {
 52        DataTable  dt= ToDataTable(json);
 53          return dt;
 54     }
 55      
 56    #endregion
 57  
 58     #region DataTable 转换为Json 字符串
 59     /// <summary>
 60     /// DataTable 对象 转换为Json 字符串
 61     /// </summary>
 62     /// <param name="dt"></param>
 63     /// <returns></returns>
 64     public static string ToJson(this DataTable dt)
 65     {
 66         JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
 67         javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
 68         ArrayList arrayList = new ArrayList();
 69         foreach (DataRow dataRow in dt.Rows)
 70         {
 71             Dictionary<string, object> dictionary = new Dictionary<string, object>();  //实例化一个参数集合
 72             foreach (DataColumn dataColumn in dt.Columns)
 73             {
 74                 dictionary.Add(dataColumn.ColumnName, dataRow[dataColumn.ColumnName].ToStr());
 75             }
 76             arrayList.Add(dictionary); //ArrayList集合中添加键值
 77         }
 78  
 79         return javaScriptSerializer.Serialize(arrayList);  //返回一个json字符串
 80     }
 81     #endregion
 82  
 83     #region Json 字符串 转换为 DataTable数据集合
 84     /// <summary>
 85     /// Json 字符串 转换为 DataTable数据集合
 86     /// </summary>
 87     /// <param name="json"></param>
 88     /// <returns></returns>
 89     public static DataTable ToDataTable(this string json)
 90     {
 91         DataTable dataTable = new DataTable();  //实例化
 92         DataTable result;
 93         try
 94         {
 95             JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
 96             javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
 97             ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json);
 98             if (arrayList.Count > 0)
 99             {
100                 foreach (Dictionary<string, object> dictionary in arrayList)
101                 {
102                     if (dictionary.Keys.Count<string>() == 0)
103                     {
104                         result = dataTable;
105                         return result;
106                     }
107                     if (dataTable.Columns.Count == 0)
108                     {
109                         foreach (string current in dictionary.Keys)
110                         {
111                             dataTable.Columns.Add(current, dictionary[current].GetType());
112                         }
113                     }
114                     DataRow dataRow = dataTable.NewRow();
115                     foreach (string current in dictionary.Keys)
116                     {
117                         dataRow[current] = dictionary[current];
118                     }
119  
120                     dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
121                 }
122             }
123         }
124         catch
125         {
126         }
127         result = dataTable;
128         return result;
129     }
130     #endregion
131  
132     #region 转换为string字符串类型
133     /// <summary>
134     ///  转换为string字符串类型
135     /// </summary>
136     /// <param name="s">获取需要转换的值</param>
137     /// <param name="format">需要格式化的位数</param>
138     /// <returns>返回一个新的字符串</returns>
139     public static string ToStr(this object s, string format = "")
140     {
141         string result = "";
142         try
143         {
144             if (format == "")
145             {
146                 result = s.ToString();
147             }
148             else
149             {
150                 result = string.Format("{0:" + format + "}", s);
151             }
152         }
153         catch
154         {
155         }
156         return result;
157     }
158    #endregion
View Code

 

posted @ 2015-04-30 14:46  Mark1997  阅读(16679)  评论(0编辑  收藏  举报