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

转载于:https://www.cnblogs.com/sdya/p/4624410.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值