View Full Version : Resolved Using a string as a object attr name

12-29-2008, 07:57 AM
I'm trying to create a UI object that displays a list of JSON data being delivered through an AJAX request. Say the JSON is formatted like so:

{list:[{key:val},{key:val},{key:val}], date:'01/01/09' }

For the purposes of reusing this UI element, the element should take the evaluated JSON object and a string for the root list node so that it knows which data to display. Something like this:

var data = eval(jsonString);
var rootnode = 'list';

var Example = function(data,rootnode) {
for (item in data.rootnode) {
// add 'item' to the html list

In the code above, obviously 'rootnode' can't be used like it is. However, since the JSON is evaluated into an object, how can I use the value of 'rootnode' to access the correct attribute in data??

12-29-2008, 08:12 AM
In this situation it worked like this: data[rootnode]