2012-01-17 281 views
2

我需要將從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()); 
     } 
+2

什麼是你的問題? – ShankarSangoli 2012-01-17 15:45:50

+0

你能編輯和發佈你用來創建(錯誤)JSON對象的C#代碼嗎?看看引號和空格,你會發現它們不匹配:''CustomerID「:」25「Title」:「」Mr.「 「名字」:「」唐納德「」'... – msanford 2012-01-17 15:47:36

+0

對不起。張貼錯誤的JSON ..已糾正 – 2012-01-17 15:48:13

回答

2

如果你得到json然後指定dataType等於json as 4th argument in $不用彷徨will parse the json which you can iterate using the each`的jQuery的方法,像

$.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'); 

更新

the 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 
     } 
    ] 
} 
+0

會嘗試讓你知道.. – 2012-01-17 15:52:27

+0

+1。當然,如果JSON格式正確,這仍然只能工作。 – StriplingWarrior 2012-01-17 16:02:16

+0

現在預計錯誤 - }:P json已被更新超過 – 2012-01-17 16:15:26

0

你需要兩個類-call他們ROVAL和客戶,爲示例 - 並將其定義爲這樣:

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); 
+0

我必須在asp 1.1中開發我的應用程序,它沒有javascriptserializer – 2012-01-17 15:50:43

+2

如果可能的話(我不知道JSON.NET是否支持1.1)使用JSON.NET。你應該相應地標記你的問題。 – Icarus 2012-01-17 15:52:06