1    #region  数据表DataTable 转键值对集合 List
2 /// <summary>
3 /// 数据表DataTable 转键值对集合 List
4 /// 把DataTable转成 List集合, 存每一行
5 /// 集合中放的是键值对字典,存每一列
6 /// </summary>
7 /// <param name="dt">数据表</param>
8 /// <returns>哈希表数组</returns>
9 public static List<Dictionary<string, object>> DataTableToList(DataTable dt)
10 {
11 List<Dictionary<string, object>> list
12 = new List<Dictionary<string, object>>();
13
14 foreach (DataRow dr in dt.Rows)
15 {
16 Dictionary<string, object> dic = new Dictionary<string, object>();
17 foreach (DataColumn dc in dt.Columns)
18 {
19 dic.Add(dc.ColumnName, dr[dc.ColumnName]);
20 }
21 list.Add(dic);
22 }
23 return list;
24 }
25 #endregion
26
27 #region 数据集转键值对数组字典
28
29 /// <summary>
30 /// 数据集转键值对数组字典
31 /// </summary>
32 /// <param name="dataSet">数据集</param>
33 /// <returns>键值对数组字典</returns>
34 public static Dictionary<string, List<Dictionary<string, object>>> DataSetToDic(DataSet ds)
35 {
36 Dictionary<string, List<Dictionary<string, object>>> result = new Dictionary<string, List<Dictionary<string, object>>>();
37
38 foreach (DataTable dt in ds.Tables)
39 result.Add(dt.TableName, DataTableToList(dt));
40
41 return result;
42 }
43 #endregion
44
45 #region 数据表Tables 转JSON
46 /// <summary>
47 /// 数据表转Tables JSON
48 /// </summary>
49 /// <param name="dataTable">数据表</param>
50 /// <returns>JSON字符串</returns>
51 public static string DataTableToJSON(DataTable dt)
52 {
53 return JsonHelper.ObjectToJSON(DataTableToList(dt));
54 }
55 #endregion
56
57 #region 将datatable转换为json
58 /// <summary>
59 /// 将datatable转换为json
60 /// </summary>
61 /// <param name="dtb">Dt</param>
62 /// <returns>JSON字符串</returns>
63 public static string Dtb2Json(DataTable dtb)
64 {
65 //JavaScriptSerializer jss = new JavaScriptSerializer();
66
67 System.Collections.ArrayList dic = new System.Collections.ArrayList();
68
69 foreach (DataRow dr in dtb.Rows)
70 {
71 System.Collections.Generic.Dictionary<string, object> drow = new System.Collections.Generic.Dictionary<string, object>();
72
73 foreach (DataColumn dc in dtb.Columns)
74 {
75 drow.Add(dc.ColumnName, dr[dc.ColumnName]);
76 }
77
78 dic.Add(drow);
79 }
80
81 return null;
82 }
83 #endregion
84
85 #region 将Dictionary转换为数据表数据 Tables
86 public static DataTable DictToDataTable(Dictionary<string, object> dict)
87 {
88 DataTable dt = new DataTable();
89
90 //dt.Columns.Add("ID", typeof(Guid));
91 //dt.Columns.Add("DID", typeof(string));
92 //dt.Columns.Add("DEPARTMENTNUM", typeof(string));
93 //dt.Columns.Add("DEPARTMENTNAME", typeof(string));
94 //dt.Columns.Add("REMARKS", typeof(string));
95
96 foreach (var colName in dict.Keys)
97 {
98 dt.Columns.Add(colName, typeof(string));
99 }
100 DataRow dr = dt.NewRow();
101 foreach (KeyValuePair<string, object> item in dict)
102 {
103 dr[item.Key] = item.Value;
104 }
105 dt.Rows.Add(dr);
106 return dt;
107 }
108 #endregion
109
110 #region 将List转换为数据表数据 Tables
111 /// <summary>
112 /// List转DataTable
113 /// </summary>
114 public static DataTable ListToDataTable<T>(List<T> list)
115 {
116 if (list == null || list.Count == 0)
117 {
118 return new DataTable();
119 }
120
121 //获取T下所有的属性
122 Type entityType = list[0].GetType();
123 PropertyInfo[] entityProperties = entityType.GetProperties();
124
125 DataTable dt = new DataTable("data");
126
127 for (int i = 0; i < entityProperties.Length; i++)
128 {
129 dt.Columns.Add(entityProperties[i].Name);
130 }
131
132 foreach (var item in list)
133 {
134 if (item.GetType() != entityType)
135 {
136 throw new Exception("要转换集合元素类型不一致!");
137 }
138 //创建一个用于放所有属性值的数组
139 object[] entityValues = new object[entityProperties.Length];
140 for (int i = 0; i < entityProperties.Length; i++)
141 {
142 entityValues[i] = entityProperties[i].GetValue(item, null);
143 }
144
145 dt.Rows.Add(entityValues);
146 }
147 return dt;
148 }
149 #endregion
150
151 #region Json 字符串 转换为 DataTable数据集合 简要版,正在使用中
152 /// <summary>
153 /// Json 字符串 转换为 DataTable数据集合 简要版,正在使用中
154 /// </summary>
155 /// <param name="json"></param>
156 /// <returns></returns>
157 ///
158 //格式;
159 //[{"mac":"20:f1:7c:c5:cd:80","rssi":"-86","ch":"9"},{"mac":"20:f1:7c:c5:cd:85","rssi":"-91","ch":"9"}]
160 public static DataTable ToDataTableTwo(string json)
161 {
162 DataTable dataTable = new DataTable(); //实例化
163 DataTable result;
164 try
165 {
166 List<Dictionary<string, object>> arrayList = JsonHelper.JSONToObject<List<Dictionary<string, object>>>(json);
167
168 if (arrayList != null && arrayList.Count > 0)
169 {
170 foreach (Dictionary<string, object> dictionary in arrayList)
171 {
172 if (dictionary.Keys.Count == 0)
173 {
174 result = dataTable;
175 return result;
176 }
177
178 //Columns
179 if (dataTable.Columns.Count == 0)
180 {
181 foreach (var current in dictionary)
182 {
183 if (current.Value != null)
184 {
185 dataTable.Columns.Add(current.Key, current.Value.GetType());
186 }
187 else
188 {
189 dataTable.Columns.Add(current.Key);
190 }
191 }
192 }
193
194 //Rows
195 DataRow dataRow = dataTable.NewRow();
196
197 foreach (string current in dictionary.Keys)
198 {
199 if (dictionary[current] != null)
200 {
201 dataRow[current] = dictionary[current];
202 }
203 }
204
205 dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
206 }
207 }
208 }
209 catch (Exception ex)
210 {
211 throw ex;
212 }
213
214 result = dataTable;
215 return result;
216 }
217 #endregion

C#DataTable(转List /JSON/字典 互转)的更多相关文章

  1. DataTable 和Json 字符串互转

    #region DataTable 转换为Json字符串实例方法 /// <summary> /// GetClassTypeJosn 的摘要说明 /// </summary> ...

  2. 二:C#对象、集合、DataTable与Json内容互转示例;

    导航目录: Newtonsoft.Json 概述 一:Newtonsoft.Json 支持序列化与反序列化的.net 对象类型:    二:C#对象.集合.DataTable与Json内容互转示例: ...

  3. 将DataSet(DataTable)转换成JSON格式(生成JS文件存储)

    public static string CreateJsonParameters(DataTable dt) { /**/ /**/ /**/ /* /*********************** ...

  4. Datatable转成Json方式两则

    1, Asp.net C# 使用Newtonsoft.Json 实现DataTable转Json格式数据 1.这里下载:http://www.newtonsoft.com/products/json/ ...

  5. LINQ查询返回DataTable类型[轉]與将DataTable序列化为Json格式【轉】

    (原文地址:http://xuzhihong1987.blog.163.com/blog/static/26731587201101853740294/) LINQ查询返回DataTable类型 在使 ...

  6. DataTable 转换成 Json的3种方法

    在web开发中,我们可能会有这样的需求,为了便于前台的JS的处理,我们需要将查询出的数据源格式比如:List<T>.DataTable转换为Json格式.特别在使用Extjs框架的时候,A ...

  7. asp.net dataTable转换成Json格式

    /// <summary> /// dataTable转换成Json格式 /// </summary> /// <param name="dt"> ...

  8. Datatable/Dataset 转 JSON方法

    当数据库表的数据在一般处理程序中查出来需要将这个表数据返回到前台的jquery中,需要将数据拼成json字符串形式,这里是将数据库数据查出放在Datatable中,然后在一般处理程序中将datatab ...

  9. DataTable转换成json字符串

    将DataTable里面的行转换成json字符串方法: #region DataTable转为json /// <summary> /// DataTable转为json /// < ...

  10. JSON格式互转集合

    在工作中我们经常会遇到格式转换的问题,有的时候是将JSON转换成DataTable.DataSet或是List等,也有可能将DataTable.DataSet或是List转换成JSON的,抽了点时间把 ...

随机推荐

  1. 【笔记】入门DP

    复习一下近期练习的入门 \(DP\) .巨佬勿喷.\(qwq\) 重新写一遍练手,加深理解. 代码已经处理,虽然很明显,但请勿未理解就贺 \(qwq\) 0X00 P1057 [NOIP2008 普及 ...

  2. perl静态变量

    state操作符功能类似于C里面的static修饰符,state关键字将局部变量变得持久. state也是词法变量,所以只在定义该变量的词法作用域中有效,举个例子: #!/usr/bin/perl u ...

  3. RabbitMq发布确认

    RabbitMq发布确认 发布确认原理 生产者将信道设置成 confirm 模式,一旦信道进入 confirm 模式,所有在该信道上面发布的消息都将会被指派一个唯一的 ID(从 1 开始),一旦消息被 ...

  4. js和jquery页面初始化加载函数的方法及顺序

    运行下面代码.弹出A.B.C.D.E的顺序:A=B=C>D=E. <html> <head> <title>首页</title> <scri ...

  5. fuzor2020安装教程

    fuzor下载安装包fuzor2020安装教程Fuzor2020 WIN10 64位安装步骤:1.先使用"百度网盘客户端"下载Fur20_CN_x64安装包到电脑磁盘里,并鼠标右击 ...

  6. 原来用 MySQL 也可以做全文检索

    我是风筝,公众号「古时的风筝」,专注于 Java技术 及周边生态. 文章会收录在 JavaNewBee 中,更有 Java 后端知识图谱,从小白到大牛要走的路都在里面. 有朋友聊到他们的系统中要接入全 ...

  7. i春秋Zone

    打开网页是个简单的表单填写, 尝试注入....没用 查看源码,没找到什么有用的信息 只有抓包了 发现一个cookie的login值为0,改为1试试 没什么特别的回显,但这应该就是登录与否的判定了,所以 ...

  8. audio解决不能自动播放问题

    问题描述 无法实现打开网页就能自动播放音乐 正常情况下使用autoplay即可实现自动播放,但是现在打开网页该参数无效 原因分析: 根据最新的规范,Chrome系浏览器,没有交互过的网站默认禁止自动播 ...

  9. nginx配置文件单独创建和管理

    在nginx主配置文件nginx.conf的http模块下引入配置文件夹(注意路径的正确性) 1.nginx主配置文件备份后编辑(nginx配置存放位置:/usr/local/nginx/conf/) ...

  10. 【HBase】简介、结构、数据模型、快速入门部署、shell操作、架构原理、读写数据流程、数据刷写、压缩、分割、Phoenix、表的映射、与hive集成、优化

    一.简介 1.定义 分布式.可扩展.支持海量数据存储的NoSQL数据库 2.数据模型 2.1逻辑结构 2.2物理存储结构 2.3数据模型介绍 Name Space:相当于数据库,包含很多张表 Regi ...