I need a simple help with this question, I’m trying to convert a JSON string to two arrays, look the example:
// What I have
var str = '{"id":1,"name":"Test1"},{"id":2,"name":"Test2"}';
// What I need:
var array1 = {"id":1,"name":"Test1"};
var array2 = {"id":2,"name":"Test2"};
I tried to do it using JSON.parse(str), but with no success like below:
//Works:
var str = '{"id":1,"name":"Test1"}';
dataObj = JSON.parse(str);
console.log(dataObj)
//Doesn't work:
var str = '{"id":1,"name":"Test1"},{"id":2,"name":"Test2"}';
dataObj = JSON.parse(str);
console.log(dataObj)
Any suggestion ?
>Solution :
all you want to do is put the objects in array
var str = '[{"id":1,"name":"Test1"},{"id":2,"name":"Test2"}]';
dataObj = JSON.parse(str);
console.log(dataObj)
then:
var array1 = dataObj[0];
var array2 = dataObj[1];