1

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"

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

in your last code block, it should be function(name, result). name being the key, result being the value of that key.
I accepted the first part, since it turns out that I could just access the json object directly. Stupid of me to not realize that in the first place.
Alright, in MooTools it's reversed so I'm used to that model.
+1 for telling me data['results']['dataset_a']['dataset_two']['data']; way of calling json objects.
the square bracket are accessors which can be used on arrays arr = [1]; arr[0] and objects obj = { foo : 'hello world'}; obj['foo']; x = 'foo'; obj[x]
0

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 !-)

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.