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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With