我通过解码 JSON 数据得到了这个输出。
现在我想反转这个数据结构并使用 Perl 打印它。
任何人都可以帮助我使用下面的代码片段吗?
我将这些数据保存在一个变量中并打印出来。我的输出如下图所示
我有一个看起来像这样的哈希值:
$VAR1 = {
'Packet Loss to Source' => {
'142' => {
'161' => '0.000',
'162' => '0.000',
'141' => '0.000'
}
},
'Packet Loss to Destination' => {
'142' => {
'161' => '0.000',
'162' => '0.000',
'141' => '0.000'
}
},
'Average Jitter to Source' => {
'142' => {
'161' => '13.323',
'162' => '37.003',
'141' => '192.309'
}
},
'Availability' => {
'142' => {
'161' => '0.000',
'162' => '0.000',
'141' => '1.042'
}
},
'Average Round Trip Time' => {
'142' => {
'161' => '772.278',
'162' => '389.566',
'141' => '8557.511'
}
},
'Average Jitter to Destination' => {
'142' => {
'161' => '13.323',
'162' => '37.003',
'141' => '192.309'
}
}
};
这是我试图获取输出的代码。我阅读文本文件并获取 JSON 数据,对其进行解码并打印。我在变量中得到了上述输出$perl_obj
。
#my $dirname = "/home/brix/ravikiran/doc/demo.txt";
my $dirname = "/home/brix/ravikiran/doc/JSONData.txt";
open HANDLE, $dirname;
my @raw_data = <HANDLE>;
my $json_text;
foreach my $row (@raw_data) {
$json_text .= $row;
}
close(HANDLE);
print "$json_text;\n";
my $json = JSON::XS->new();
$json = json->pretty(1)->
space_before(1)->
space_after(1)->
canonical(1)->
allow_blessed(1)->
convert_blessed(1);
my $perl_obj = $json->decode($json_text);
print STDOUT Dumper($perl_obj);