I have a method in my model that queries my SQL Server database and stores the results in a datatable. Then, I am using the following method to convert that datatable into a JSON string.
public void ConvertDataTabletoJSONString(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 (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
JSONstring = serializer.Serialize(rows);
}
My controller looks like this:
public ActionResult getMonthlyData()
{
TDR_Monthly viewModel = new TDR_Monthly();
viewModel.getList(0);
return Json(viewModel.JSONstring, JsonRequestBehavior.AllowGet);
}
My view's javascript AmCharts.makechart dataLoader has this:
"dataLoader":
{
"url": "../TDR_MonthlyController/getMonthlyData",
"format": "json"
}
When I "test" the output of that string (by displaying the string's contents in my view, it shows properly in the browser, like so:
[{"REGION":"Atlanta", "STATE_NAME":"Alabama", "STATE":"AL", "CATEGORY_ID":"0 ", "CATEGORY":"No Group", "COUNT":100, "DEFICIENCY1":0, "DEFICIENCY2":0, "RESCIND1":0, "RESCIND2":0}]
However, when I modify the browser's URL to execute the JSON call (http://localhost:49777/Monthly/getMonthlyData) and I open the JSON file, it looks like it has a crap tone of extra spacing and characters:
"[{\"REGION\":\"Atlanta \",\"STATE_NAME\":\"Alabama \",\"STATE\":\"AL\",\"CATEGORY_ID\":\"0 \",\"CATEGORY\":\"No Group \",\"COUNT\":100,\"DEFICIENCY1\":0,\"DEFICIENCY2\":0,\"RESCIND1\":0,\"RESCIND2\":0}]
The view itself informs me that it cannot parse it
Error parsing JSON file: ../TDR_MonthlyController/getMonthlyData
Any help would be appreciated!!!