Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON.parse Uncaught SyntaxError: Unexpected token o [duplicate]

I have this JSON :

var data =  [{
    "ID":1,"Name":"Test",
    "subitem": [
        {"idenID":1,"Code":"254630"},
        {"idenID":2,"Code":"4566"},
        {"idenID":3,"Code":"4566"}
    ]
}];

console.log(JSON.parse(data)); //Uncaught SyntaxError: Unexpected token o 

How to de-serialize data to javascript object.

like image 679
titi Avatar asked Sep 24 '13 02:09

titi


2 Answers

It already is an object ... of type Array. To access the Object:

var foo = data[0];

alert(foo.ID);

JSON.parse takes a String and parses it into an equivalent JavaScript value.

like image 135
Alex Avatar answered Sep 25 '22 09:09

Alex


This is usable in Javascript. You need to parse JSON when your data is in String format and you get it from server side.

The purpose of JSON.parse is to convert to Javascipt Object Notation to use it. For example,

var str = "{"a":1,"b":2}";
var obj = JSON.parse(str); //obj = {a:1, b:2}

Reference MDN

like image 42
Jhankar Mahbub Avatar answered Sep 25 '22 09:09

Jhankar Mahbub