JSON.parse:意外的字符错误

时间:2012-04-03 20:46:35

标签: javascript json

我遇到了问题。我在单独的文件中有JSON对象列表,但想要将它们解析为数据表。每次我尝试解析它们时,都会出现意外的字符错误......

这是代码

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 个答案:

答案 0 :(得分:6)

JSON是(JavaScript)对象的字符串表示。 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 已经一个对象,不需要“解析”。

答案 1 :(得分:0)

这是我遇到的一个问题,解决方案与双引号的使用有关。

http://mywpf-visu.blogspot.in/2012/04/json-encountered-unexpected-character.html