0

我有问题。我在一个单独的文件中有 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);
4

2 回答 2

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已经是一个对象,不需要“解析”。

于 2012-04-03T20:50:19.527 回答
0

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

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

于 2012-04-04T20:24:03.457 回答