关于json动态拼接响应数据

在EasyUI http://www.jeasyui.com/demo/main/get_users.php 响应数据如下格式:

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
{
    "total": "11",
    "rows": [
        {
            "id": "50528",
            "firstname": "wrerw",
            "lastname": "erwerw",
            "phone": "",
            "email": ""
        },
        {
            "id": "50529",
            "firstname": "asdf",
            "lastname": "asdf",
            "phone": "",
            "email": "asdf@asdf.c"
        },
        {
            "id": "50532",
            "firstname": "sdfdsfds",
            "lastname": "fsdfdsfagcdga",
            "phone": "",
            "email": ""
        },
        {
            "id": "50533",
            "firstname": "ewfg",
            "lastname": "gdfgdfgdfg",
            "phone": "",
            "email": ""
        },
        {
            "id": "50534",
            "firstname": "sagfg",
            "lastname": "wwGSGSG",
            "phone": "",
            "email": ""
        },
        {
            "id": "50535",
            "firstname": "sa",
            "lastname": "sa",
            "phone": "sa",
            "email": "sa@dssddsd.dsd"
        },
        {
            "id": "50536",
            "firstname": "asdf",
            "lastname": "asdf",
            "phone": "asdf",
            "email": "asdf@asdf.com"
        },
        {
            "id": "50537",
            "firstname": "s",
            "lastname": "sdf",
            "phone": "",
            "email": ""
        },
        {
            "id": "50538",
            "firstname": "33",
            "lastname": "sd",
            "phone": "13445",
            "email": "131@131.com"
        },
        {
            "id": "50539",
            "firstname": "1",
            "lastname": "1",
            "phone": "1",
            "email": "1@qq.com"
        }
    ]
}

  //C#后台动态拼接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
DataTable dt = new DataTable();
           StringBuilder str = new StringBuilder();
           str.Append("{\"total\":\"" + dt.Rows.Count + "\",\"rows\":[");
           for (int i = 0; i < dt.Rows.Count; i++)
           {
                 //就是这些\ 转义很容易造成错误
            str.Append("{\"FtyVendorCode\":\"" + dt.Rows[i]["CustID"].ToString() + "\",\"CompanyName\":\"" + dt.Rows[i]["CompanyName"].ToString() + "\",\"Contact\":\"" + dt.Rows[i]["ContactPerson"].ToString() + "\",\"EMail\":\"" + dt.Rows[i]["ContactEmail"].ToString() + "\",\"Phone\":\"" + dt.Rows[i]["ContactPhone"].ToString() + "\"");
                 
           }
 
           str.ToString().TrimEnd(new char[] { ',' });
           str.Append("]}");
 
           context.Response.Write(str.ToString());

  

posted @   KyrieYang  阅读(2816)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示