我需要將從C#代碼獲得的一組值存儲到一個javascript數組中。 我得到一個錯誤..有人可以告訴錯誤是什麼? 即時通訊使用此jcode。解析JSON數組對象
$.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');
我正在使用ajax查詢來檢索JSON對象的數組。我得到的數據對象值就像字符串一樣。
{"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}]}
這裏是生成JSON我的C#代碼
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());
}
什麼是你的問題? – ShankarSangoli 2012-01-17 15:45:50
你能編輯和發佈你用來創建(錯誤)JSON對象的C#代碼嗎?看看引號和空格,你會發現它們不匹配:''CustomerID「:」25「Title」:「」Mr.「 「名字」:「」唐納德「」'... – msanford 2012-01-17 15:47:36
對不起。張貼錯誤的JSON ..已糾正 – 2012-01-17 15:48:13