I have a get method to get the data from a JSON tree:
var tree = { user: [{ name: 'name1', email:'email1' }, {name: 'name2', email:'email2'}] };
getJSONNodeData(tree, 'user[1].name');
function getJSONNodeData(root, path) {
try {
return (new Function('root', 'return root.' + path + ';'))(root);
} catch (e) {return "JSON Error";}
}
I would like to set the JSON data using a similar simple method. I expected this to work:
function setJSONNodeData(root, path, newValue) {
return (new Function('root', 'return root.' + path + '=' + newValue +';'))(root);
}
But this does not work? Why and what will? The Get works but the Set does not.