easyui 数据列表的数据源格式


easyui datagrid 的json数据有严格的格式,必须有total 和rows 例如:
{ "total":条数,    
   "rows":[     {"id01":"282.32","id02":"122221","id03":"0329281","id04":"29292918"},
                    {"id01":"282.32","id02":"122221","id03":"0329281","id04":"29292918"},
                    {"id01":"282.32","id02":"122221","id03":"0329281","id04":"29292918"}
            ]
}

c#后台方法:
 public static string DataTableToJson(string jsonName, int rowCount, DataTable dt)
        {
            StringBuilder Json = new StringBuilder();
            Json.Append("{\"total\":" + rowCount + ",\"" + jsonName + "\":[");
            if (dt.Rows.Count > 0)
            {
                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(",");
                    }
                }
            }
            Json.Append("]}");
            return Json.ToString();
        }

 

posted @ 2015-08-10 11:11  石中隐鱼  阅读(574)  评论(0编辑  收藏  举报