JSON.parse Uncaught SyntaxError: Unexpected token o

I have a 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 disinfect data on a javascript object.

+6
source share
2 answers

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

 var foo = data[0]; alert(foo.ID); 

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

+12
source

This can be used in Javascript. You need to parse JSON when your data is in String format and you will get it from the server side.

The purpose of JSON.parse is to convert to a Javascipt Object Designation to use it. For instance,

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

MDN Link

+2
source

Source: https://habr.com/ru/post/954472/


All Articles