我有一个正在解码的 json 结构,如下所示:
person => {
city => "Chicago",
id => 123,
name => "Joe Smith",
pets => {
cats => [
{ age => 6, name => "cat1", type => "siamese", weight => "10 kilos" },
{ age => 10, name => "cat2", type => "siamese", weight => "13 kilos" },
],
dogs => [
{ age => 7, name => "dog1", type => "siamese", weight => "20 kilos" },
{ age => 5, name => "dog2", type => "siamese", weight => "15 kilos" },
],
},
},
}
我可以通过以下方式打印city
, id
:name
foreach my $listing ($decoded->{person})
{
my $city = $listing->{city};
my $name = $listing->{name};
name - $city - \n";
}
但是,我不确定如何打印pets->cats
or pets->dogs
。我可以通过以下方式对它们进行转储:
my @pets = $listing->{pets}->{cats};
dump @pets;
但我不确定如何通过哈希结构访问它们。