支付宝
微信
扫描二维码打赏
更多详情(暂无)

Json转换成DataTable

   今天看到Json转DataTable的例子,总结一下。。。。

 1 using System;
 2 using System.Collections;
 3 using System.Collections.Generic;
 4 using System.Data;
 5 using System.IO;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Web;
10 using System.Web.Script.Serialization;
11 namespace Json转DT_List
12 {
13 
14     class Program
15     {
16         static void Main(string[] args)
17         {
18             StreamReader sr = new StreamReader(@"F:\vs2015Test\VS2015学习\Json转DT,List\json1.json", Encoding.Default);
19             var jsonStr = sr.ReadToEnd();
20             var Dt = JsonToDatatable("");
21             var str = DataTableToJson(new DataTable());
22             Console.WriteLine(str);
23 
24             Console.ReadKey();
25         }
26 
27         public static DataTable JsonToDatatable(string json)
28         {
29             DataTable datatable = null;
30             if (!string.IsNullOrEmpty(json))
31             {
32                 JavaScriptSerializer jss = new JavaScriptSerializer();
33                 ArrayList list = jss.Deserialize<ArrayList>(json);//将指定的 JSON 字符串转换为 T 类型的对象,可以是自己的List对象
34                 datatable = new DataTable();
35                 if (list.Count > 0)
36                 {
37                     foreach (Dictionary<string, object> ll in list)//定义一个.Dictionary盛放list中的对象
38                     {
39                         if (datatable.Columns.Count == 0)
40                         {
41                             foreach (var key in ll.Keys)
42                                 datatable.Columns.Add(key, ll[key].GetType());   //添加新列                              
43                         }
44                         var newRow = datatable.NewRow();
45                         foreach (var k in ll.Keys)
46                         {
47                             newRow[k] = ll[k];//给行上的列赋值
48                         }
49                         datatable.Rows.Add(newRow);
50                     }
51                 }
52             }
53             return datatable;
54         }
55 
56         public static string DataTableToJson(DataTable dt)
57         {
58             var jsonStr = "";
59             if (dt != null)
60             {
61                 ArrayList arrlist = new ArrayList();
62                 JavaScriptSerializer jss = new JavaScriptSerializer();
63                 foreach (DataRow row in dt.Rows)//遍历DataTable的Rows要是用DataRow
64                 {
65                     Dictionary<string, object> dicDrow = new Dictionary<string, object>();
66                     foreach (DataColumn c in dt.Columns)//遍历DataTable的Columns要使用DataColumn
67                         dicDrow.Add(c.ColumnName, row[c.ColumnName]);
68                     arrlist.Add(dicDrow);
69                 }
70                 jsonStr = jss.Serialize(arrlist);
71             }
72             return jsonStr;
73         }
74     }
75 }

 

 

该方法只能转换JSON数组格式的字符串,JSON对象格式只能转换成对象。。。。。

附带json1.json格式

[
  {
    "CFTUin": "769839263",
    "CancelDeadline": "2013-09-12 23:00:00",
    "CheckInDate": "2013-09-12 00:00:00",
    "CheckOutDate": "2013-09-13 00:00:00",
    "CityID": "0101",
    "CurrencyCode": "RMB",
    "HotelID": "00301105",
    "HotelName": "乐家连锁(北京天坛南门店)(原速8酒店(北京天坛南门店)",
    "ListID": "1000000005201308280002999652",
    "PayAmt": "228",
    "PayType": "0",
    "RommsCnt": "1",
    "SPTransID": "65202157",
    "State": "4"
  },
  {
    "CFTUin": "248486133",
    "CancelDeadline": "2013-10-13 23:00:00",
    "CheckInDate": "2013-10-13 00:00:00",
    "CheckOutDate": "2013-10-18 00:00:00",
    "CityID": "0201",
    "CurrencyCode": "RMB",
    "HotelID": "10201314",
    "HotelName": "上海凯顿酒店",
    "ListID": "1000000005201308280002999413",
    "PayAmt": "1140",
    "PayType": "0",
    "RommsCnt": "1",
    "SPTransID": "65197226",
    "State": "4"
  }
]

 

posted @ 2017-08-08 14:05  华临天下  阅读(3467)  评论(0编辑  收藏  举报