【引用】DataTable 的 JSON 序列化
来源:互联网 发布:京东全球购奶粉 知乎 编辑:程序博客网 时间:2024/04/30 17:52
DataTable 的 JSON 序列化
public string CreateJsonParameters(DataTable dt)
{
/**//* /****************************************************************************
* Without goingin to the depth of the functioning of this Method, i will try to give an overview
* As soon as this method gets a DataTable it starts to convert it into JSON String,
* it takes each row and in each row it grabs the cell name and its data.
* This kind of JSON is very usefull when developer have to have Column name of the .
* Values Can be Access on clien in this way. OBJ.HEAD[0].<ColumnName>
* NOTE: One negative point. by this method user will not be able to call any cell by its index.
* *************************************************************************/
StringBuilder JsonString = new StringBuilder();
//Exception Handling
if (dt != null && dt.Rows.Count > 0)
{
JsonString.Append("{ ");
JsonString.Append("\"Head\":[ ");
for (int i = 0; i < dt.Rows.Count; i++)
{
JsonString.Append("{ ");
for (int j = 0; j < dt.Columns.Count; j++)
{
if (j < dt.Columns.Count - 1)
{
JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\",");
}
else if (j == dt.Columns.Count - 1)
{
JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\"");
}
}
/**//*end Of String*/
if (i == dt.Rows.Count - 1)
{
JsonString.Append("} ");
}
else
{
JsonString.Append("}, ");
}
}
JsonString.Append("]}");
return JsonString.ToString();
}
else
{
return null;
}
}
随着AJAX,MVC等WEB框架的使用,JavaScript又更多的回到了我们身边。
在JS中我们需要对对象进行JSON序列化通常使用JSON.net, 不过它对DataTable的序列化不能很好的满足的我们的需求,后来在CodeProject发现一个兄弟已经写好了 Convert ASP.NET DataTable to JSON, to use datatable in JAVASCRIPT ,记一下,免得忘记了。
public string CreateJsonParameters(DataTable dt)
{
/**//* /****************************************************************************
* Without goingin to the depth of the functioning of this Method, i will try to give an overview
* As soon as this method gets a DataTable it starts to convert it into JSON String,
* it takes each row and in each row it grabs the cell name and its data.
* This kind of JSON is very usefull when developer have to have Column name of the .
* Values Can be Access on clien in this way. OBJ.HEAD[0].<ColumnName>
* NOTE: One negative point. by this method user will not be able to call any cell by its index.
* *************************************************************************/
StringBuilder JsonString = new StringBuilder();
//Exception Handling
if (dt != null && dt.Rows.Count > 0)
{
JsonString.Append("{ ");
JsonString.Append("\"Head\":[ ");
for (int i = 0; i < dt.Rows.Count; i++)
{
JsonString.Append("{ ");
for (int j = 0; j < dt.Columns.Count; j++)
{
if (j < dt.Columns.Count - 1)
{
JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\",");
}
else if (j == dt.Columns.Count - 1)
{
JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\"");
}
}
/**//*end Of String*/
if (i == dt.Rows.Count - 1)
{
JsonString.Append("} ");
}
else
{
JsonString.Append("}, ");
}
}
JsonString.Append("]}");
return JsonString.ToString();
}
else
{
return null;
}
}
0 0
- 【引用】DataTable 的 JSON 序列化
- DataTable 的 JSON 序列化
- DataTable 的 JSON 序列化
- DataTable序列化Json
- 解决MVC Json序列化的循环引用问题/EF Json序列化循引用问题(Newtonsoft.Json)
- [IgnoreDataMember]:json序列化循环引用的问题
- DataTable序列化成Json字符串
- DataTable序列化为JSON字符串
- EntityFramework中Json序列化的循环引用问题解决--Newtonsoft.Json
- nodejs 循环引用JSON序列化错误的解决办法:TypeError: Converting circular structure to JSON
- json的序列化
- 将DataTable序列化为Json格式
- 序列化问题!Datatable
- 序列化DataTable
- 序列化DataTable
- 序列化---对象引用的序列化
- EntityFramework中JSON序列化循环引用----JavaScriptSerializer
- JSON序列化的方法
- js与iframe操作
- C# 委托delege的处理
- 【引用】ComboBox WinForm开发系列 -
- treeview 递归实现
- asp.net 下载程序文件
- 【引用】DataTable 的 JSON 序列化
- table 边框显示设置和js显示无图片样式
- REDIS 学习(9)读写分离,redis主从配置
- jquery + json 操作
- Delphi中StrToDateTime函数TFormatSettings参数的使用
- Linq to DataTable 操作
- 锁存器不爱触发器
- php返回json数据
- ASP.NET中使用System.Net.Mail发邮件