admin管理员组

文章数量:1291061

I'm having a problem. I have the list of JSON objects in a separate file but want to parse them into a data table. Every time I try to parse them, I get an unexpected character error...

Here is the code

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

I'm having a problem. I have the list of JSON objects in a separate file but want to parse them into a data table. Every time I try to parse them, I get an unexpected character error...

Here is the code

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);
Share Improve this question edited Apr 3, 2012 at 20:47 Colin Brock 21.6k9 gold badges49 silver badges62 bronze badges asked Apr 3, 2012 at 20:46 RHammondsRHammonds 1071 gold badge1 silver badge8 bronze badges 2
  • 2 In your example, myJSONObject is already an object, it doesn't need to be parsed. – gen_Eric Commented Apr 3, 2012 at 20:48
  • 2 I don't think you understand what JSON is. JSON.parse will convert a string to an object. You already have an object. – Alex Turpin Commented Apr 3, 2012 at 20:48
Add a ment  | 

2 Answers 2

Reset to default 6

JSON is a string representation of a (JavaScript) object. A JSON string, is a valid JavaScript object.

Example:

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

In your example, myJSONObject is already an object, it doesn't need to be "parsed".

This is one problem i had faced and the solution is related to usage of double quotes.

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

本文标签: javascriptJSONparse unexpected character errorStack Overflow