2012-04-03 109 views
0

我遇到問題。我有一個單獨的文件中的JSON對象的列表,但想分析它們到一個數據表中。每次我嘗試分析他們,我得到一個意外的字符錯誤...JSON.parse:意外字符錯誤

下面是代碼

var myJSONObject = { 
       "orders" : [{ 
        "orderId" : "K2_001", 
        "dueDate" : "04/15/2012", 
        "priority" : 1, 
        "description" : "ORDER K2_001" 
       }, { 
        "orderId" : "K2_002", 
        "dueDate" : "04/20/2012", 
        "priority" : 2, 
        "description" : "ORDER K2_002" 
       }, { 
        "orderId" : "K2_003", 
        "dueDate" : "04/23/2012", 
        "priority" : 3, 
        "description" : "ORDER K2_003" 
       }, { 
        "orderId" : "K2_004", 
        "dueDate" : "04/27/2012", 
        "priority" : 4, 
        "description" : "ORDER K2_004" 
       }, { 
        "orderId" : "K2_005", 
        "dueDate" : "04/30/2012", 
        "priority" : 5, 
        "description" : "ORDER K2_005" 
       }, { 
        "orderId" : "K2_006", 
        "dueDate" : "05/05/2012", 
        "priority" : 6, 
        "description" : "ORDER K2_006" 
       }, { 
        "orderId" : "K2_007", 
        "dueDate" : "05/12/2012", 
        "priority" : 7, 
        "description" : "ORDER K2_007" 
       }, { 
        "orderId" : "K2_008", 
        "dueDate" : "05/14/2012", 
        "priority" : 8, 
        "description" : "ORDER K2_008" 
       }] 
      }; 
      var jsonObject2 = Y.JSON.parse(myJSONObject.responseText); 
+2

在你的榜樣,'myJSONObject'是* *已經一個對象,它不需要被解析。 – 2012-04-03 20:48:16

+2

我不認爲你明白JSON是什麼。 'JSON.parse'將把一個字符串轉換爲一個對象。你已經有一個對象。 – 2012-04-03 20:48:47

回答

6

JSON是(JavaScript的)對象的字符串表示。 A JSON 字符串,是一個有效的JavaScript 對象

例子:

var JSON = '{"Hello": "world", "test": [1,2,3]}'; // <= This is JSON, it's a string 
var obj = {"Hello": "world", "test": [1,2,3]}; // <= This is a JavaScript object 

在你的榜樣,myJSONObject已經一個對象,它並不需要 「的解析」。

相關問題