Json DataTable

在JS中我們需要對對象進行JSON序列化通常使用JSON.net, 不過它對DataTable的序列化不能很好的滿足的我們的需求,
後來在CodeProject發現一個兄弟已經寫好了 Convert ASP.NET DataTable to JSON, to use datatable in JAVASCRIPT ,記一下,免得忘記了。
 
public string JsonToDataTable(DataTable dt)
        {
            /****************************************************************************
             * Without goingin to the depth of the functioning of this Method, i will try to give an overview
             * As soon as this method gets a DataTable it starts to convert it into JSON String,
             * it takes each row and ineach row it creates an array of cells and in each cell is having its data
             * on the client side it is very usefull for direct binding of object to  TABLE.
             * Values Can be Access on clien in this way. OBJ.TABLE[0].ROW[0].CELL[0].DATA 
             * NOTE: One negative point. by this method user will not be able to call any cell by its name.
             * *************************************************************************/
            StringBuilder JsonString = new StringBuilder();
            JsonString.Append("{ ");
            JsonString.Append("/"TABLE/":[{ ");
            JsonString.Append("/"ROW/":[ ");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                JsonString.Append("{ ");
                JsonString.Append("/"COL/":[ ");
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    if (j < dt.Columns.Count - 1)
                    {
                        JsonString.Append("{" + "/"DATA/":/"" + dt.Rows[i][j].ToString() + "/"},");
                    }
                    else if (j == dt.Columns.Count - 1)
                    {
                        JsonString.Append("{" + "/"DATA/":/"" + dt.Rows[i][j].ToString() + "/"}");
                    }
                }
                /*end Of String*/
                if (i == dt.Rows.Count - 1)
                {
                    JsonString.Append("]} ");
                }
                else
                {
                    JsonString.Append("]}, ");
                }
            }
            JsonString.Append("]}]}");
            return JsonString.ToString();
        }


        public string DataTableToJson(DataTable dt)
        {          
           /*****************************************************************************
            * Without goingin to the depth of the functioning of this Method, i will try to give an overview
            * As soon as this method gets a DataTable it starts to convert it into JSON String,
            * it takes each row and in each row it grabs the cell name and its data.
             * This kind of JSON is very usefull when developer have to have Column name of the .
            * Values Can be Access on clien in this way. OBJ.HEAD[0].
            * NOTE: One negative point. by this method user will not be able to call any cell by its index.
            * *************************************************************************/
            StringBuilder JsonString = new StringBuilder();
            //Exception Handling        
            if (dt != null && dt.Rows.Count > 0)
            {
                JsonString.Append("{ ");
                JsonString.Append("/"Head/":[ ");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    JsonString.Append("{ ");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        if (j < dt.Columns.Count - 1)
                        {
                            JsonString.Append("/"" + dt.Columns[j].ColumnName.ToString() + "/":" + "/"" + dt.Rows[i][j].ToString() + "/",");
                        }
                        else if (j == dt.Columns.Count - 1)
                        {
                            JsonString.Append("/"" + dt.Columns[j].ColumnName.ToString() + "/":" + "/"" + dt.Rows[i][j].ToString() + "/"");
                        }
                    }
                    /**/
                    /*end Of String*/
                    if (i == dt.Rows.Count - 1)
                    {
                        JsonString.Append("} ");
                    }
                    else
                    {
                        JsonString.Append("}, ");
                    }
                }
                JsonString.Append("]}");
                return JsonString.ToString();
            }
            else
            {
                return null;
            }
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章