我需要遍历几个json并打印出每个的索引1 ...
我从生成的包含我的JSON数据的字符串开始。然后,我解码该字符串并将其转储以确切显示我正在使用的内容:
my $decoded = decode_json $string
print Dumper $string
结果为以下输出:
$VAR1 = [
{
'hdr' => [
1,
'acknowledged',
'',
'/home/clanier/dev/test/sds-test/data/JPLIDR2015169.64575',
'2015/271-19:10:39.0101355',
'2015/271-19:10:39.2599252',
''
]
},
{
'hdr' => [
2,
'acknowledged',
'',
'/home/clanier/dev/test/sds-test/data/JPLIDR2015169.64575',
'2015/271-19:10:39.3928414',
'2015/271-19:10:39.6397269',
''
]
},
{
'hdr' => [
3,
'acknowledged',
'',
'/home/clanier/dev/test/sds-test/data/JPLIDR2015169.64575',
'2015/271-19:10:39.7726375',
'2015/271-19:10:40.0162758',
''
]
}
];
现在,我尝试遍历此过程,并打印每个单词的确认词:
foreach my $hdr ( $decoded->{hdr} ) {
print $hdr->[1];
}
我曾向该解决方案寻求帮助,但由于“不是HASH参考”错误,看来我什至不能达到原始海报的水平。我以前可以打印特定的纸,但是我需要遍历并打印所有纸。这是该代码:print $$decoded[0]->{'hdr'}->[1];
$decoded
是数组引用,而不是哈希引用。您不能将其取消引用为哈希:->{
。
您可以将其作为数组取消引用:
for my $hdr (@$decoded) {
print $hdr->{hdr}[1];
}
有关Perl中引用的更多信息,请参见perlreftut。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句