DataTable To JSON 序列化的两种方法

  1A)
  2public string JSON_DataTable(DataTable dt)
  3        {
  4            /****************************************************************************
  5
  6             * Without goingin to the depth of the functioning of this Method, i will try to give an overview
  7
  8             * As soon as this method gets a DataTable it starts to convert it into JSON String,
  9
 10
 11 
 12
 13             * it takes each row and ineach row it creates an array of cells and in each cell is having its data
 14
 15             * on the client side it is very usefull for direct binding of object to  TABLE.
 16
 17
 18             * Values Can be Access on clien in this way. OBJ.TABLE[0].ROW[0].CELL[0].DATA 
 19
 20             * NOTE: One negative point. by this method user will not be able to call any cell by its name.
 21
 22             * *************************************************************************/
 
 23            StringBuilder JsonString = new StringBuilder(); 
 24
 25            JsonString.Append("");
 26            JsonString.Append("\"TABLE\":[{ ");
 27            JsonString.Append("\"ROW\":[ ");
 28            for (int i = 0; i < dt.Rows.Count; i++)
 29            {
 30                JsonString.Append("");
 31                JsonString.Append("\"COL\":[ ");
 32                for (int j = 0; j < dt.Columns.Count; j++)
 33                {
 34                    if (j < dt.Columns.Count - 1)
 35                    {
 36                        JsonString.Append("{" + "\"DATA\":\"" + dt.Rows[i][j].ToString() + "\"},");
 37                    }

 38                    else if (j == dt.Columns.Count - 1)
 39                    {
 40
 41                        JsonString.Append("{" + "\"DATA\":\"" + dt.Rows[i][j].ToString() + "\"}");
 42                    }

 43                }

 44                /*end Of String*/
 45                if (i == dt.Rows.Count - 1)
 46                {
 47                    JsonString.Append("]} ");
 48                }

 49                else
 50                {
 51                    JsonString.Append("]}, ");
 52                }

 53            }

 54            JsonString.Append("]}]}");
 55            return JsonString.ToString();
 56
 57        }

 58B)
 59        public string CreateJsonParameters(DataTable dt)
 60        {
 61
 62            /* /****************************************************************************
 63
 64             * Without goingin to the depth of the functioning of this Method, i will try to give an overview
 65
 66             * As soon as this method gets a DataTable it starts to convert it into JSON String,
 67
 68             * it takes each row and in each row it grabs the cell name and its data.
 69
 70             * This kind of JSON is very usefull when developer have to have Column name of the .
 71
 72             * Values Can be Access on clien in this way. OBJ.HEAD[0].<ColumnName>
 73
 74             * NOTE: One negative point. by this method user will not be able to call any cell by its index.
 75
 76             * *************************************************************************/

 77            StringBuilder JsonString = new StringBuilder();
 78            //Exception Handling        
 79            if (dt != null && dt.Rows.Count > 0)
 80            {
 81                JsonString.Append("");
 82                JsonString.Append("\"Head\":[ ");
 83                for (int i = 0; i < dt.Rows.Count; i++)
 84                {
 85                    JsonString.Append("");
 86                    for (int j = 0; j < dt.Columns.Count; j++)
 87                    {
 88                        if (j < dt.Columns.Count - 1)
 89                        {
 90                            JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\",");
 91                        }

 92                        else if (j == dt.Columns.Count - 1)
 93                        {
 94                            JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\"");
 95                        }

 96                    }

 97                    /*end Of String*/
 98                    if (i == dt.Rows.Count - 1)
 99                    {
100                        JsonString.Append("");
101                    }

102                    else
103                    {
104                        JsonString.Append("}, ");
105                    }

106                }

107                JsonString.Append("]}");
108                return JsonString.ToString();
109            }

110            else
111            {
112                return null;
113            }

114        }

115    }
原文地址:https://www.cnblogs.com/sonicit/p/1051195.html