正在分析对象的JSON数组

本文关键字:JSON 数组 对象 | 更新日期: 2023-09-27 18:20:46

我需要将从C#代码中获得的一组值存储到javascript数组中。我遇到一个错误。。有人能说出错误是什么吗?我正在使用此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');

我使用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}]}

这是我的C#代码,它正在生成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());
        }

正在分析对象的JSON数组

如果要获取json,则将dataType指定为等于json as 4th argument in$.get-will parse the json which you can iterate using theeach`methodofjquery,如

 $.get('Dataextract.aspx', function (data, textStatus) {
        alert('Status is ' + textStatus);//success
        alert('JSON data string is: ' + data);//string as below
    // no need for eval
   // var JSONdata = data;
   // eval(JSONdata);//error here-> expected ;
    alert(JSONdata.rowval[0].CustomerID);
}, "json"); // <-- 

或者可以像一样显式解析json

 $.get('Dataextract.aspx', function (data, textStatus) {
        alert('Status is ' + textStatus);//success
        alert('JSON data string is: ' + data);//string as below
    var JSONdata = $.parseJSON(data);
   // eval(JSONdata);//error here-> expected ;  again no need for the eval
    alert(JSONdata.rowval[0].CustomerID);
}, 'text');

更新

您正在形成的json不正确,为了验证您的json,您可以访问www.jsonlint.com,这是有效的json

{
    "rowval": [
        {
            "CustomerID": 12,  // <-- you are missing the commas 
            "Title": "Mr.",
            "FirstName": "Johnny",
            "MiddleName": "A.",
            "LastName": "Caprio",
            "CompanyName": "Bikes and Motorbikes",
            "RowNumber": 10
        }
    ]
}

您需要两个类,例如称它们为RoVal和Customer,并将它们定义为:

class RowVal
{
    public List<Customer> Customers {get;set;}
}
class Customer
{
   public int ID {get;set;}
   public string Title{get;set;}
   public string FirstName {get;set;}
   //and so on
}

现在您可以将JSON字符串反序列化为

JavascriptSerializer serializer = new JavascriptSerializer();
RowVal rowVal =    serializer.Deserialize<RowVal>(yourJSonstring);