从Perl中的JSON中提取数据

时间:2015-07-17 16:02:29

标签: perl

以下是我的60MB JSON文件示例:

[
   {
      "phish_id":"3332444",
      "url":"http://shydroservice.ru/plugins/content/fboxbot/standardbank3/inet.php",
      "phish_detail_url":"http://www.phishtank.com/phish_detail.php?phish_id=3332444",
      "submission_time":"2015-07-17T09:58:13+00:00",
      "verified":"yes",
      "verification_time":"2015-07-17T10:14:15+00:00",
      "online":"yes",
      "details":[
         {
            "ip_address":"37.140.192.240",
            "cidr_block":"37.140.192.0/24",
            "announcing_network":"197695",
            "rir":"ripencc",
            "country":"RU",
            "detail_time":"2015-07-17T09:59:28+00:00"
         }
      ],
      "target":"Other"
   },
   ...
]

我想获得ip_address的{​​{1}},但我的代码找不到details数组:'(

details

我有这样的信息:

use strict;
use warnings FATAL => 'all';
use LWP::Simple qw(get);
use JSON qw(decode_json);

my $url = "http://127.0.0.1/test.json";

my $decoded = decode_json(get($url));

foreach my $f ( @decoded ) {
  print $f->{"details"} . "\n";
}

任何人都可以帮助我吗? 感谢。

1 个答案:

答案 0 :(得分:5)

在顶层,您有[],因此$decoded是对数组的引用。

for my $f (@$decoded) {
   ...
}

在你的内部{},所以$f是对哈希的引用。

   my $details = $f->{details};

在你的内部[],所以$details是对数组的引用。

   for my $detail (@$details) {
      ...
   }

在你的内部{},所以$detail是对哈希的引用。

      my $ip_address = $detail->{ip_address};

所有在一起:

for my $f (@$decoded) {
   my $details = $f->{details};
   for my $detail (@$details) {
      my $ip_address = $detail->{ip_address};
      ...
   }
}