JSON.parse: unexpected character error

Go To StackoverFlow.com

0

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);
2012-04-03 20:46
by RHammonds
In your example, myJSONObject is already an object, it doesn't need to be parsed - Rocket Hazmat 2012-04-03 20:48
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 2012-04-03 20:48


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".

2012-04-03 20:50
by Rocket Hazmat


0

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

2012-04-04 20:24
by Vish
You should summarise the solution in the link in case the link is not available in the future - Richard Dalton 2012-04-05 12:21