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

        }

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。