关于json动态拼接响应数据

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

{
    "total": "11",
    "rows": [
        {
            "id": "50528",
            "firstname": "wrerw",
            "lastname": "erwerw",
            "phone": "",
            "email": ""
        },
        {
            "id": "50529",
            "firstname": "asdf",
            "lastname": "asdf",
            "phone": "",
            "email": "[email protected]"
        },
        {
            "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": "[email protected]"
        },
        {
            "id": "50536",
            "firstname": "asdf",
            "lastname": "asdf",
            "phone": "asdf",
            "email": "[email protected]"
        },
        {
            "id": "50537",
            "firstname": "s",
            "lastname": "sdf",
            "phone": "",
            "email": ""
        },
        {
            "id": "50538",
            "firstname": "33",
            "lastname": "sd",
            "phone": "13445",
            "email": "[email protected]"
        },
        {
            "id": "50539",
            "firstname": "1",
            "lastname": "1",
            "phone": "1",
            "email": "[email protected]"
        }
    ]
}

  //C#后台动态拼接

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

  

你可能感兴趣的:(json)