Getting JSON data from server could not be parsed (JSON formatting error).

Getting JSON data from server could not be parsed (JSON formatting error).

jgelinas33jgelinas33 Posts: 0Questions: 0Answers: 0
edited October 2012 in General
Using ie9, C#, VS2010 as dev environment:

JSON value being returned in response from server code is:

{"sEcho": 1,"iTotalRecords": 9,"iTotalDisplayRecords": 9,"
aaData":[
{"VendorName": "ATG","SevenDays": 8214,"LastDay": 5,"LastHour": 4,"Rejects": 423},
{"VendorName": "AzPay","SevenDays": 23,"LastDay": 0,"LastHour": 0,"Rejects": 14},
{"VendorName": "BedBath","SevenDays": 43,"LastDay": 0,"LastHour": 0,"Rejects": 9},
{"VendorName": "E4X","SevenDays": 103,"LastDay": 0,"LastHour": 0,"Rejects": 6},
{"VendorName": "eBay","SevenDays": 32,"LastDay": 0,"LastHour": 0,"Rejects": 10},
{"VendorName": "EDI","SevenDays": 15,"LastDay": 0,"LastHour": 0,"Rejects": 10},
{"VendorName": "Mercent","SevenDays": 27,"LastDay": 0,"LastHour": 0,"Rejects": 9},
{"VendorName": "Shop","SevenDays": 11,"LastDay": 0,"LastHour": 0,"Rejects": 0},
{"VendorName": "Trickle","SevenDays": 14,"LastDay": 0,"LastHour": 0,"Rejects": 0}]}


Client Code (html page):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">







@import"/Styles/demo_table.css";
@import"/Sytels/demo_page.css";




var oTable;

$(document).ready
(
function () {
oTable = $("#table_id").dataTable({
"bProcessing": true,
"bServerSide" : true,
"sAjaxSource": "WebForm1.aspx",
"aoColumns": [
{ "mData": "VendorName" },
{ "mData": "SevenDays" },
{ "mData": "LastDay" },
{ "mData": "LastHour" },
{ "mData": "Rejects" }
]
});
$("#table_id tbody tr").click(
function () {
if ($(this).hasClass('row_selected')) {
$(this).removeClass('row_selected');
}
else {
oTable.$('tr.row_selected').removeClass('row_selected');
$(this).addClass('row_selected');
}
}
);
}
);












Vendor
Past 7 Days
Past 24 Hours
Past Hour
Rejects Outstanding




ATG
2153
143
38
10


ATG
2153
143
38
10









Server code getting data and creating JSON Response in PageLoad event of aspx page (as per examples given in documentation):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace DataTablesInWebApplication
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
SqlConnection dbConnection;
SqlCommand dbCommand;
SqlDataAdapter dbAdapter;
DataTable dbTable = new DataTable();
string connectionString;
string dtJSONString = "";

connectionString = "Server=HQSQLDEVSRVR;Database=Tapemacs;User ID=sqlrunner;Password=d8tabase;";
dbConnection = new SqlConnection(connectionString);
dbConnection.Open();
dbCommand = new SqlCommand("spget_weborderstatussummary", dbConnection);
dbCommand.CommandType = CommandType.StoredProcedure;
dbAdapter = new SqlDataAdapter(dbCommand);
dbAdapter.Fill(dbTable);

dtJSONString = @"{";
dtJSONString = dtJSONString + @"""sEcho"": " + Request["sEcho"] + ",";
dtJSONString = dtJSONString + @"""iTotalRecords"": " + Convert.ToString(dbTable.Rows.Count) + ",";
dtJSONString = dtJSONString + @"""iTotalDisplayRecords"": " + Convert.ToString(dbTable.Rows.Count) + ",";
dtJSONString = dtJSONString + @"""aaData"":[";

foreach (DataRow row in dbTable.Rows)
{
dtJSONString = dtJSONString + @"{";
dtJSONString = dtJSONString + @"""VendorName"": " + @"""" + row.Field("VendorName").ToString() + @"""" + ",";
dtJSONString = dtJSONString + @"""SevenDays"": " + Convert.ToString(row.Field("SevenDays")) + ",";
dtJSONString = dtJSONString + @"""LastDay"": " + Convert.ToString(row.Field("LastDay")) + ",";
dtJSONString = dtJSONString + @"""LastHour"": " + Convert.ToString(row.Field("LastHour")) + ",";
dtJSONString = dtJSONString + @"""Rejects"": " + Convert.ToString(row.Field("Rejects")) ;
dtJSONString = dtJSONString + @"},";
}

dtJSONString = dtJSONString.Substring(0, dtJSONString.Length - 1);
dtJSONString = dtJSONString + "]}";

Response.Write(dtJSONString);
}

}
}


Any ideas why the above indicated JSON that is returned is not satisfactory for the DataTables table definition?
This discussion has been closed.