I keep getting this error:
SyntaxError: Unexpected token o
For a v.simple piece of code:
var temp = {"1":["2","1","0000-00-00 00:00:00","testing once"],"2":["2","1","0000-00-00 00:00:00","testing twice :)"]};
console.log(JSON.parse(temp)); //error is here
var temp is the json_encoded data.. yet i get this error =/ what does it mean i got wrong?
The problem here is that that is not JSON. That is a Javascript object. (The syntax for JSON is derived from that of a Javascript object.) It does not need to be parsed into a Javascript object because it already is one.
Just do
console.log(temp)and you’ll see this.The reason you get this strange message is that trying to do
JSON.parseon an object makes Javascript first attempt to convert the object into a string. The string that results is unhelpful:"[object Object]".JSON.parseattempts to parse this. It copes with[but falls down on the firsto, since that is illegal JSON syntax.