in reply to Array / Hash Confusion!

Store the result of the JSON decode into a scalar:
my $data = decode_json($response); print "$data->{title} was released in year -> $data->{year}, and it is + rated $data->{rating}\n";

Replies are listed 'Best First'.
Re^2: Array / Hash Confusion!
by andy503 (Initiate) on Jan 07, 2014 at 19:56 UTC

    thanks for your reply. I understand I need to store the result in a scalar and thanks for your suggested code, which I've seen works in my env.

    it clearly works for "$data->{title}"; as this is a single result, but when "$data->{genres}"; this returns ARRAY(gobblydegook) as expected.

    how do I effectively test if a result is an array or not, and then jump into it? or should I be converting this into a hash and trying to look at key/value combinations?

      ref is handy:
      foreach my $key (keys %$data) { my $thing = $data->{$key}; if (ref $thing eq 'HASH') { useThingAsHashRef($thing); }elsif (ref $thing eq 'ARRAY') { useThingAsArray($thing); } }
      If you know it is an array:
      print "$_\n" for @{ $data->{genres} };

      See also: