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);
2
  • 2
    In your example, myJSONObject is already an object, it doesn't need to be parsed. 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. Commented Apr 3, 2012 at 20:48

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

You should summarise the solution in the link in case the link is not available in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.