I have to code below:
#!/usr/intel/bin/perl
use strict;
use warnings;
use JSON::XS;
my $json = '{"Object1":{"Year":"2012","Quarter":"Q3","DataType":"Other 3","Environment":"STEVE","Amount":125},"Object2":{"Year":"2012","Quarter":"Q4","DataType":"Other 2","Environment":"MIKE","Amount":500}}';
my $arrayref = decode_json $json;
for my $array(@$arrayref){
for my $key (keys(%$array)){
my $val = $array->{$key};
print "$key: $val\n";
}
}
When I compile it, it print me the error "Not an ARRAY reference at generator.pl line 12.".
I want to parse the JSON to an object and get data according to the object with the attributes. How can I do it?
I expect after I parse it, I can use to compare string, print, loop it and so on.
refbuiltin. Or print the whole thing by one of the number of modules for working with complex data structure,Data::Dumperfor example.reftells you whether it isHASHorARRAY(or other) or not a reference -- see docs forref. The top-level is probably going to be hashref (this is not strictly required), and in your case that's just what you have. Then you iterate overkeys %$hashref(what you call$arrayref). Or,use Data::Dumper; print Dumper $reference;to see the whole thing that is in$reference.