Datatable 生成json格式

?





1

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

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();

        }

  

时间: 2024-08-29 14:42:31

Datatable 生成json格式的相关文章

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

由于项目用到了JSON格式的数据,在网上搜索到了阿里的fastjson比较好用,特此记录fastjson用法,以备以后查询之用. decode: 首先创建一个JSON解析类: public class JsonParser { private int devid; public int getDevid() { return devid; } public void setDevid(int devid) { this.devid = devid; } } 在需要用到解析的地方创建一个对应的对象

Java Servlet生成Json格式数据

Java Servlet生成Json格式数据 分类: Web JAVA2013-09-17 14:38 4805人阅读 评论(1) 收藏 举报 在Servlet中覆写doGet方法,是用JSONStringer 类: [java] view plaincopyprint? protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 

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

1.这里下载:http://www.newtonsoft.com/products/json/ 安装:    1.解压下载文件,得到Newtonsoft.Json.dll    2.在项目中添加引用 2.引入命名空间 using Newtonsoft.Json; using Newtonsoft.Json.Converters; 3.把datatable转换成json格式 public string GetAllCategory() { string result = ""; Data

在VS2013下编译json-c库,并简单生成json格式数据

#include "stdafx.h"#include "json-c/json.h" int _tmain(int argc, _TCHAR* argv[]){ // 正常的json格式 json_object *json = json_object_new_object(); json_object_object_add(json, "name", json_object_new_string("laomeng")); j

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

使用map构建json格式的数据 使用java bean来构建json对象

生成json格式

html页面 <input type="button" value="重新生成JSON" class="button1" id="createjson" /> javascript部分 <script type="text/javascript"> $(function () { //生成JSON $("#createjson").click(function (

DataTable转换为Json格式

public string DataTableToJson(string jsonName, DataTable dt, TemplateFilter temp)         { StringBuilder Json = new StringBuilder(); Json.Append("["); Json.Append("{"); //当前页码 Json.Append("\"pageIndex\":\"" +

app开发历程————服务器端生成JSON格式数据,采用Unicode编码,隐藏中文

今天,问以前的同事,他们写接口按什么编码,怎么看到有\u的一些看不懂的内容,一问,原来是信息隐藏,防止信息泄漏. 然后在网上查了Java如何把中文转换成unicode编码,转自:http://blog.csdn.net/sunmenggmail/article/details/27539023 1 package mobi.chenwei.wing.util; 2 3 public class CharacterSetToolkit { 4 5 /** 6 * @param args 7 */

eclipse 自动生成json格式的toString()方法

文本代码 {"${member.name()}":"${member.value}", "${otherMembers}"} 原文地址:https://www.cnblogs.com/duanwandao/p/9655282.html