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);
JSON.parse
will convert a string to an object. You already have an object - Alex Turpin 2012-04-03 20:48
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
myJSONObject
is already an object, it doesn't need to be parsed - Rocket Hazmat 2012-04-03 20:48