I need to store a set of values which I get from my C# code to a javascript array. Im getting an error .. Can someone tell what the error is ? Im using this jcode.
$.get('Dataextract.aspx', function (data, textStatus) {
alert('Status is ' + textStatus);//success
alert('JSON data string is: ' + data);//string as below
var JSONdata = data;
eval(JSONdata);//error here-> expected ;
alert(JSONdata.rowval[0].CustomerID);
}, 'text');
I am using an ajax query to retrieve an array of JSON object. the data objects value that i get is something like this as string.
{"rowval" :[{"CustomerID":12"Title":"Mr.""FirstName":"Johnny""MiddleName":"A.""LastName":"Caprio""CompanyName":"Bikes and Motorbikes""RowNumber":10},{"CustomerID":16"Title":"Mr.""FirstName":"Christopher""MiddleName":"R.""LastName":"Beck""CompanyName":"Bulk Discount Store""RowNumber":11},{"CustomerID":18"Title":"Mr.""FirstName":"David""MiddleName":"J.""LastName":"Liu""CompanyName":"Catalog Store""RowNumber":12},{"CustomerID":19"Title":"Mr.""FirstName":"John""MiddleName":"A.""LastName":"Beaver""CompanyName":"Center Cycle Shop""RowNumber":13},{"CustomerID":20"Title":"Ms.""FirstName":"Jean""MiddleName":"P.""LastName":"Handley""CompanyName":"Central Discount Store""RowNumber":14},{"CustomerID":21"Title":"FirstName":"Jinghao""MiddleName":"LastName":"Liu""CompanyName":"Chic Department Stores""RowNumber":15},{"CustomerID":22"Title":"Ms.""FirstName":"Linda""MiddleName":"E.""LastName":"Burnett""CompanyName":"Travel Systems""RowNumber":16},{"CustomerID":23"Title":"Mr.""FirstName":"Kerim""MiddleName":"LastName":"Hanif""CompanyName":"Bike World""RowNumber":17},{"CustomerID":24"Title":"Mr.""FirstName":"Kevin""MiddleName":"LastName":"Liu""CompanyName":"Eastside Department Store""RowNumber":18},{"CustomerID":25"Title":"Mr.""FirstName":"Donald""MiddleName":"L.""LastName":"Blanton""CompanyName":"Coalition Bike Company""RowNumber":19},{"CustomerID":28"Title":"Ms.""FirstName":"Jackie""MiddleName":"E.""LastName":"Blackwell""CompanyName":"Commuter Bicycle Store""RowNumber":20}]}
Here is my C# code that is generating the JSON
sb.Append("{\"rowval\" :");
sb.Append("[");
if (table != null)
{
foreach (DataRow row in table.Rows)
{
sb.Append("{");
if (row.Table != null && row.Table.Columns != null && row.Table.Columns.Count > 0)
{
foreach (DataColumn column in row.Table.Columns)
{
parseMember(row[column], column.ColumnName, sb);
}
}
sb.Append("},");
}
}
sb.Append("]");
sb.Append("}");
sqlcon.Close();
Response.Write(sb);
}
private static void parseMember(object val, string memberName, StringBuilder sb)
{
Type t = val.GetType();
if (memberName != null && memberName.Trim().Length > 0)
sb.AppendFormat("\"{0}\":", memberName);
if (typeof(string) == t || typeof(char) == t)
sb.AppendFormat("\"{0}\"", val.ToString());
else
sb.AppendFormat("{0}", val.ToString());
}
if you are getting
json
then specifyingdataType
equal tojson as 4th argument in
$.getwill parse the json which you can iterate using the
each` method of jquery, likeor you can parse the json explicitly like
update
the json you are forming is not correct, for validating your json you can visit www.jsonlint.com, this is the valid json