首页 > 代码库 > DataTable转换为Json格式

DataTable转换为Json格式

public string DataTableToJson(string jsonName, DataTable dt, TemplateFilter temp)         {

            StringBuilder Json = new StringBuilder();

            Json.Append("[");

            Json.Append("{");            

//当前页码            

Json.Append("\"pageIndex\":\"" + temp.PageIndex.ToString() + "\",");            

//总条数           

Json.Append("\"pagenum\":\"" + Convert.ToInt32(Math.Ceiling(Convert.ToDouble(temp.AllPageNum) / Convert.ToDouble(temp.PageSize))) + "\",");

            //搜索结果            

Json.Append("\"result\":\"" + temp.Results + "\"");

 

            if (dt.HasData())             {                

Json.Append("},");               

for (int i = 0; i < dt.Rows.Count; i++)                 {                    

Json.Append("{");

                    for (int j = 0; j < dt.Columns.Count; j++)                     {

                        Json.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":\"" + dt.Rows[i][j].ToString() + "\"");

                        if (j < dt.Columns.Count - 1)                         {

                            Json.Append(",");

                        }

                    }

                    Json.Append("}");

                    if (i < dt.Rows.Count - 1)                     {

                        Json.Append(",");

                    }

                }             }             else             {                 Json.Append("}");             }

 

            Json.Append("]");

            return Json.ToString();

        }

DataTable转换为Json格式