public string GetJsonFromDataTable(DataTable dt, int total, bool ShowFooter, string fields, string inputfiled, string ShowMessage = "合计:", string filter = "")
{
StringBuilder stringBuilder = new StringBuilder();
if (dt.Rows.Count == 0)
{
stringBuilder.Append("{ ");
stringBuilder.Append("\"rows\":[ ");
stringBuilder.Append("]");
stringBuilder.Append(",");
stringBuilder.Append("\"total\":");
stringBuilder.Append(total);
stringBuilder.Append(",\"footer\":[");
stringBuilder.Append("]");
stringBuilder.Append("}");
return stringBuilder.ToString();
}
stringBuilder.Append("{ ");
stringBuilder.Append("\"rows\":[ ");
for (int i = 0; i < dt.Rows.Count; i++)
{
stringBuilder.Append("{ ");
for (int j = 0; j < dt.Columns.Count; j++)
{
if (j < dt.Columns.Count - 1)
{
stringBuilder.Append(string.Concat(new string[]
{
"\"",
dt.Columns[j].ColumnName.ToString().ToLower(),
"\":\"",
this.JsonCharFilter(dt.Rows[i][j].ToString()),
"\","
}));
}
else
{
if (j == dt.Columns.Count - 1)
{
stringBuilder.Append(string.Concat(new string[]
{
"\"",
dt.Columns[j].ColumnName.ToString().ToLower(),
"\":\"",
this.JsonCharFilter(dt.Rows[i][j].ToString()),
"\""
}));
}
}
}
if (i == dt.Rows.Count - 1)
{
stringBuilder.Append("} ");
}
else
{
stringBuilder.Append("}, ");
}
}
stringBuilder.Append("]");
stringBuilder.Append(",");
stringBuilder.Append("\"total\":");
stringBuilder.Append(total);
if (ShowFooter && fields.Length > 0 && inputfiled.Length > 0)
{
stringBuilder.Append(",\"footer\":[{");
stringBuilder.Append(string.Concat(new string[]
{
"\"",
inputfiled,
"\":\"<span style='color:red; font-weight:bold'>",
ShowMessage,
"</span>\","
}));
string[] array = fields.Split(new char[]
{
','
});
for (int k = 0; k < array.Length; k++)
{
if (k < array.Length - 1)
{
stringBuilder.Append(string.Concat(new object[]
{
"\"",
array[k],
"\":\"",
dt.Compute("sum(" + array[k] + ")", filter),
"\","
}));
}
else
{
stringBuilder.Append(string.Concat(new object[]
{
"\"",
array[k],
"\":\"",
dt.Compute("sum(" + array[k] + ")", filter),
"\""
}));
}
}
stringBuilder.Append("}]");
}
stringBuilder.Append("}");
return stringBuilder.ToString();
}

  

Datatable 生成json格式的更多相关文章

  1. Java Servlet生成JSON格式数据并用jQuery显示

    1.Servlet通过json-lib生成JSON格式的数据 import java.io.IOException;import java.io.PrintWriter;import java.uti ...

  2. 使用JSONObject类来生成json格式的数据

    JSONObject类不支持javabean转json 生成json格式数据的方式有: 1.使用JSONObject原生的来生成 2.使用map构建json格式的数据 3.使用javabean来构建j ...

  3. 如何使用fastJson来解析JSON格式数据和生成JSON格式数据

    由于项目用到了JSON格式的数据,在网上搜索到了阿里的fastjson比较好用,特此记录fastjson用法,以备以后查询之用. decode: 首先创建一个JSON解析类: public class ...

  4. python 生成json格式文件,并存储到手机上

    上代码 #!/usr/bin/env python # -*- encoding: utf-8 -*- import json import os import random "" ...

  5. .NET DataTable转换为JSON格式的字符串

    在进行数据传递的时候,有时我们需要通过Ajax的方式或者其他的方式传递一个数据列表,可以将DataTable或者其他形式的数据列表转换为JSON的格式,通过Ajax实体的形式进行传递. 比如说: // ...

  6. Asp.net C# 使用Newtonsoft.Json 实现DataTable转Json格式数据

    1.这里下载:http://www.newtonsoft.com/products/json/ 安装:    1.解压下载文件,得到Newtonsoft.Json.dll    2.在项目中添加引用  ...

  7. DataTable 转Json格式C#代码

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

  8. 生成json格式

    html页面 <input type="button" value="重新生成JSON" class="button1" id=&qu ...

  9. 关于dataTable 生成JSON 树

    背景: POSTGRESL + C#  + DHTMLX SUIT 一个表生成一个JSON串,这个不是很麻烦: 1.在数据库(postges)中:  json_agg(row_to_json(t)) ...

随机推荐

  1. TFS数据库分离附加经验总结

    因TFS数据库已经100多G,所在的服务器D盘已没有空间满足tfs数据库的增长速度,故必须分离复制到其它盘.在分离过程中,先后分离了ReportServer.ReportServerTempDB.Tf ...

  2. 你知道现在的.net是什么样的吗,一张图告诉你

    Here are these concepts used in an example sentence, for context: Application Framework - “Are you u ...

  3. 中国区 Azure 和全球版 Azure:功能对比

    由世纪互联运营的 Microsoft Azure(文中简称为中国区 Azure)是在中国大陆独立运营的公有云平台,与全球其他地区由微软运营的 Azure (文中简称全球版 Azure)服务在物理上和逻 ...

  4. 清理ThreadLocal

    在我很多的课程里(master.concurrency.xj-conc-j8),我经常提起ThreadLocal.它经常受到我严厉的指责要尽可能的避免使用.ThreadLocal是为了那些使用完就销毁 ...

  5. 一个具体的例子学习Java volatile关键字

    相信大多数Java程序员都学习过volatile这个关键字的用法.百度百科上对volatile的定义: volatile是一个类型修饰符(type specifier),被设计用来修饰被不同线程访问和 ...

  6. (转)MyBatis框架的学习(三)——Dao层开发方法

    http://blog.csdn.net/yerenyuan_pku/article/details/71700957 使用MyBatis开发Dao层,通常有两个方法,即原始Dao开发方法和Mappe ...

  7. HDU 4347 The Closest M Points (kdTree)

    赤果果的kdTree. 学习传送门:http://www.cnblogs.com/v-July-v/archive/2012/11/20/3125419.html 其实就是二叉树的变形 #includ ...

  8. fossil 代理设置

    C:\>fossil user new Joe C:\>fossil user default Joe 设置账户 fossil setting proxy http://-:-fossil ...

  9. Jarvis OJ-Level3-x64

    linux64位ROP技术 #!/usr/bin/env python from pwn import * elf = ELF('level3_x64') Io = remote('pwn2.jarv ...

  10. struts2的多个文件上传

                成功效果图:                 上篇文章描述了单个文件的上传和配置,下面主要讲解下不同的地方: index.jsp <head> <script ...