简单的Datatable转List,Json
这里用到了Newtonsoft.Json,下载地址:http://json.codeplex.com/
1.根据不同的Model转为对应的List
public static List<Model> ToList<Model>(this DataTable dt) where Model: class,new()
{ //创建一个属性的列表
List<PropertyInfo> prlist = new List<PropertyInfo>();
//获取Model的类型实例 反射的入口
Type t = typeof(Model);
//获得Model的所有的Public 属性 并找出Model属性和DataTable的列名称相同的属性(PropertyInfo) 并加入到属性列表
Array.ForEach<PropertyInfo>(t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) != -) prlist.Add(p); });
//创建返回的集合
List<Model> oblist = new List<Model>();
foreach (DataRow row in dt.Rows)
{
//创建Model的实例
var Modelob = new Model();
//找到对应的数据,并赋值
prlist.ForEach(p => { if (row[p.Name] != DBNull.Value) p.SetValue(Modelob, row[p.Name], null); });
//放入到返回的集合中.
oblist.Add(Modelob);
}
return oblist;
}
2.直接转为List<Hashtable>
把一些没有特定列的DataTable转为List<Hashtable>,一来方便使用到List<T>的功能,进行相关操作。二来,可以直接用MVC提供的Json()转为JsonResult。
public List<Hashtable> GetList(DataTable dt)
{
List<Hashtable> mList = new List<Hashtable>();
int count = dt.Rows.Count;
if (count > )
{
for (int i = ; i <= count-; i++)
{
Hashtable ht = new Hashtable();
foreach (DataColumn col in dt.Columns)
{
ht.Add(col.ColumnName, dt.Rows[i][col.ColumnName]);
}
mList.Add(ht);
}
}
return mList;
}
3.DataTable转Json
//方法一:先转List,在转Json,可以附带上分页
public string DtToJson( DataTable dt , int page, int rows)
{
string json = string.Empty;
IList<Hashtable> mList = new List<Hashtable>();
int count = dt.Rows.Count;
int pageCount = (page + rows) > count ? count : (page + rows);
if (pageCount > )
{
for (int i = page; i <= pageCount - ; i++)
{
Hashtable ht = new Hashtable();
foreach (DataColumn col in dt.Columns)
{
ht.Add(col.ColumnName, dt.Rows[i][col.ColumnName]);
}
mList.Add(ht);
}
json = JsonConvert.SerializeObject(new {total=count,rows= mList });
}
return json;
}
//方法二:非常简单,直接datatable转Json,但需要比较新版本的Newtonsoft.Json.dll
using Newtonsoft.Json;
using Newtonsoft.Json.Converters; public string DtToJson(DataTable dt)
{
result=JsonConvert.SerializeObject(dt, new DataTableConverter());
return result;
}
//方法三:转为List用mvc提供的Json转为Jsonresult
public ActionResult Select(int page, int rows)
{
var UserList=GetList(SelectUserTable());
var List = UserList.Skip((page - ) * rows).Take(rows);
int length = UserList.Count();
var result = Json(new { total = length.ToString(), rows = List });
result.ContentType = "text/html";
return result;
}
简单的Datatable转List,Json的更多相关文章
- LINQ查询返回DataTable类型[轉]與将DataTable序列化为Json格式【轉】
(原文地址:http://xuzhihong1987.blog.163.com/blog/static/26731587201101853740294/) LINQ查询返回DataTable类型 在使 ...
- DataTable 转换成 Json的3种方法
在web开发中,我们可能会有这样的需求,为了便于前台的JS的处理,我们需要将查询出的数据源格式比如:List<T>.DataTable转换为Json格式.特别在使用Extjs框架的时候,A ...
- asp.net dataTable转换成Json格式
/// <summary> /// dataTable转换成Json格式 /// </summary> /// <param name="dt"> ...
- Datatable/Dataset 转 JSON方法
当数据库表的数据在一般处理程序中查出来需要将这个表数据返回到前台的jquery中,需要将数据拼成json字符串形式,这里是将数据库数据查出放在Datatable中,然后在一般处理程序中将datatab ...
- DataTable转换成json字符串
将DataTable里面的行转换成json字符串方法: #region DataTable转为json /// <summary> /// DataTable转为json /// < ...
- 将DataSet(DataTable)转换成JSON格式(生成JS文件存储)
public static string CreateJsonParameters(DataTable dt) { /**/ /**/ /**/ /* /*********************** ...
- 将DataTable格式化为json字符串返回
一般用于ajax局部刷新的使用比较多,通过查询得到了DataTable数据,要想将数据放回需要将DataTable转换为json格式,以下为转换的调用函数: string json = "& ...
- ASP.NET 将DataTable解析成JSON简介
这里解析json使用的是Newtonsoft.Json.dll程序集.下面请看code: using System; using System.Collections.Generic; using S ...
- Datatable转成Json方式两则
1, Asp.net C# 使用Newtonsoft.Json 实现DataTable转Json格式数据 1.这里下载:http://www.newtonsoft.com/products/json/ ...
- .NET DataTable转化为json格式
标准的json用“分隔,不用' public static string DataSetToJson(DataTable dt) { string json = string.Empty ...
随机推荐
- 【Linux/Ubuntu学习5】Ubuntu 下android 开发,eclipse不能识别手机
ubuntu下eclipse不能识别手机解决方法: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1) 在终端运行 lsusb 会发现结果 ...
- Windows8不联网直接安装.Net 3.5 Framework的方法
把你的系统ISO加载到虚拟光驱或插入系统安装盘,找到X:\sources\sxs路径(X是你的光驱盘符).输入下面命令,盘符以D盘为例DISM /Online /Enable-Feature /Fea ...
- gcc: error trying to exec 'cc1plus': execvp: no such file or directory
最近在安装OpenCV cmake的时,出现gcc: error trying to exec 'cc1plus': execvp: no such file or directory的错误. 导致问 ...
- DNS的递归查询和迭代查询
百度运维部二面,直接懵逼的节奏 (1)递归查询 递归查询是一种DNS 服务器的查询模式,在该模式下DNS 服务器接收到客户机请求, 必须使用一个准确的查询结果回复客户机. 如果DNS 服务器本地没有存 ...
- 第五节 关于SpringMVC中Ajax的配置和应用[下午]
成熟,不是学会表达,而是学会咽下,当你一点一点学会克制住很多东西,才能驾驭好人生. 还有一周,祥云19就算结算了,一个半月的相处希望,胖先生算一个合格的老师 小白,小蔡,2婷婷,小猴,小恒,小崔,小龙 ...
- xhtml+css基础知识1
样式 行间样式:在标签里 <div style="width:400px; height:200px; background:red;">块</div> 内 ...
- 每天一道LeetCode--434. Number of Segments in a String
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...
- Git之使用
一大清早的,正在熟睡的我竟然被冻醒了,这天好冷啊,没事就先来扯一下犊子吧:我发现我养成了一个“怪”习惯:我老是一大早起来打开电脑就撸代码,到了中午和下午就去干别的事不想敲了.我们寝室的宿友总是被我敲代 ...
- 在远程系统上开发 SharePoint 应用程序
适用范围: apps for SharePoint | Office 365 | SharePoint Foundation 2013 | SharePoint Server 2013 使用远程安装的 ...
- c# 读取远程主机性能计数器
PerformanceCounter pc = new PerformanceCounter("Web Service", "Current Connections&qu ...