Leanan
Leanan

Reputation: 732

Dynamic Associative Array Creation in Javascript from JSON

It sounds a lot more complicated than it really is.

So in Perl, you can do something like this:

foreach my $var (@vars) {
  $hash_table{$var->{'id'}} = $var->{'data'};
} 

I have a JSON object and I want to do the same thing, but with a javascript associative array in jQuery.

I've tried the following:

hash_table = new Array();

$.each(data.results), function(name, result) {
  hash_table[result.(name).extra_info.a] = result.(name).some_dataset;
});

Where data is a JSON object gotten from a $.getJSON call. It looks more or less like this (my JSON syntax may be a little off, sorry):

{
  results:{
    datasets_a:{
      dataset_one:{
        data:{
          //stuff
        }
        extra_info:{
          //stuff
        }
      }
      dataset_two:{
         ...
      }
      ...
    }
    datasets_b:{
      ...
    }
  }
}

But every time I do this, firebug throws the following error:

"XML filter is applied to non-xml data"

Upvotes: 1

Views: 8482

Answers (3)

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114367

This works. Just dump it into a script block to test.

    d = {
      'results':{
       'datasets_a':{
          'dataset_one':{
            'data':{
              'sample':'hello'
            },
            'extra_info':{
              //stuff
            }
          },
      'dataset_two':{
            ///
          }
          ///
    },
        'datasets_b':{
         ///
        }
      }
}
alert(d.results.datasets_a.dataset_one.data.sample)

I hope this pasted in correctly. This editor doesn't like my line breaks in code.

d = {
  'results':{
   'datasets_a':{
      'dataset_one':{
        'data':{
          'sample':'hello'
        },
        'extra_info':{
          //stuff
        }
      },
      'dataset_two':{
        ///
      }
      ///
    },
    'datasets_b':{
     ///
    }
  }
};

alert(d.results.datasets_a.dataset_one.data.sample)

Upvotes: 0

Robert K
Robert K

Reputation: 30328

I think you can use the JSON response as an associative array. So you should be able to go directly in and use the JSON.

Assuming you received the above example:

$('result').innerHTML = data['results']['dataset_a']['dataset_two']['data'];
// Or the shorter form:
$('result').innerHTML = data.results.dataset_a.dataset_two.data;

Understand that I haven't tested this, but it's safer to use the square brackets with a variable than it is to use parenthesis plus the name with the dot accessor.

Your example is failing because of some convoluted logic I just caught.

$.each(data.results), function(name, result) {
     hash_table[result.(name).extra_info.a] = result.(name).some_dataset;
});

Now, the foreach loop goes through the variable data.results to find the internal elements at a depth of 1. The item it finds is given to the lambda with the key of the item. AKA, the first result will be name = "datasets_a" item = object. Following me so far? Now you access the returned hash, the object in item, as though it has the child key in name ... "datasets_a". But wait, this is the object!

If all else fails... write your result JSON into a text field dynamically and ensure it is formatted properly.

Upvotes: 3

roenving
roenving

Reputation: 2636

Why would you want to change an array into another array ?-)

-- why not simply access the data, if you want to simplify or filter, you can traverse the arrays of the object directly !-)

Upvotes: 0

Related Questions