CoderInside CoderInside - 1 year ago 123
Javascript Question

JQuery string to Object Array

I call the function tabulator() with these params.

$("#tableObj").tabulator("addRow", {id:1, Name:"John", Age:"20"}, true);

I want to pass the Array elements name dynamically,
read from a Json
( '{id:1, Name:"John", Age:"20"}' )

I mean that column names will change.
Ex :
{id:1, Company:"myComp", Address:"myaddress"}

How can I create theses objs from Strings or JSon text?

Answer Source

You could use JSON.parse but be aware that id:1, Name:"John", Age:"20" is NOT valid JSON. The keys must be wrapped in quotes, otherwise it will produce an error.

var str = '{"id":1, "Name":"John", "Age":"20"}';
var obj = JSON.parse(str);

$("#tableObj").tabulator("addRow", obj, true);