I am returning a query from the database to DataTable
and serializing it to Json
.
I use the following method to Serialize my DataTable to Json.
public static string ConvertDataTabletoString(DataTable dt)
{
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row;
foreach (System.Data.DataRow dr in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (System.Data.DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
return serializer.Serialize(rows);
}
And I have the following result:
[{"name": "DIFS - GEMS - GAECS - Eng. Man. Eq. Conventional "," date ":" [96] "}, {" name ":" DIFS - GEMS - GAECS - Eng. Man "," Conventional "," data ":" [95.51] "}, {" name ":" DIFS - GEMS - GAECS - Eng. Man. Conventional "," data ":" [89.7] "}, {" name ":" DIFS - GEMAS - GAECS - Eng. Man. Eq. Conventional "," date ":" [97.05] "}, {" name ":" DIFS - GEMS - GAECS - Eng. Man "," Conventional "," data ":" [86.86] "}, {" name ":" DIFS - GEMS - GAECS - Eng. Man. Conventional "," data ":" [90] "}, {" name ":" DIFS - GEMAS - GAECS - Eng. Man. Conventional "," data ":" [47.35] "}]
My code is right, you can see it here at JSFIELD.
Find out that the only thing wrong would be in the data
property, which instead of "data":[86.86]
is "data":"[86.86]"
. Is there any way to control this?