2012-04-03 104 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已经一个对象,它并不需要 “的解析”。