0

我有一个包含以下代码的文本文件。我想创建 perl 代码来读取该包含并在 perl 中形成相应的数据结构。我可以用下面的 perl 代码读取.txt文件,但是如何在 perl 数据结构中读取和保存所有这些Arrays of arrays/Hashes of arrays/Arrays of hashes/Hashes of hashes,有人可以帮助我吗?

{
   "Availability" : {
      "142" : {
         "141" : "1.042",
         "161" : "0.000",
         "162" : "0.000"
      }
   },
   "Average Jitter to Destination" : {
      "142" : {
         "141" : "192.309",
         "161" : "13.323",
         "162" : "37.003"
      }
   },
   "Average Jitter to Source" : {
      "142" : {
         "141" : "192.309",
         "161" : "13.323",
         "162" : "37.003"
      }
   },
   "Average Round Trip Time" : {
      "142" : {
         "141" : "8557.511",
         "161" : "772.278",
         "162" : "389.566"
      }
   },
   "Packet Loss to Destination" : {
      "142" : {
         "141" : "0.000",
         "161" : "0.000",
         "162" : "0.000"
      }
   },
   "Packet Loss to Source" : {
      "142" : {
         "141" : "0.000",
         "161" : "0.000",
         "162" : "0.000"
      }
   }

对于上面我写的代码:

print "content-type: text/html \n\n"; #HTTP HEADER
$dirname = "/ravikiran/html/JSONData.txt";
$dirpath = "$ENV{DOCUMENT_ROOT}$dirname";
sysopen(HANDLE, $dirpath , O_RDWR);
my %data;`enter code here`
while($line = <HANDLE>) {
chomp $line;
print $line;
}
close (HANDLE);
4

1 回答 1

3

输入看起来像 JSON。使用适当的模块:

use JSON;
my $s = from_json(join q(), <>);
于 2013-04-09T11:01:41.253 回答