使用Perl从Pingdom打印URL

时间:2016-10-06 16:09:21

标签: perl pingdom

我循环查看从Pingdom"获取详细检查信息" -API返回的已解码JSON。我试图在JSON数据中打印URL,但我很难这样做。

以下是我收到的JSON响应:

{
  "check" : {
    "id" : 85975,
    "name" : "My check 7",
    "resolution" : 1,
    "sendtoemail" : false,
    "sendtosms" : false,
    "sendtotwitter" : false,
    "sendtoiphone" : false,
    "sendnotificationwhendown" : 0,
    "notifyagainevery" : 0,
    "notifywhenbackup" : false,
    "created" : 1240394682,
    "type" : {
      "http" : {
        "url" : "/",
        "port" : 80,
        "requestheaders" : {
          "User-Agent" : "Pingdom.com_bot_version_1.4_(http://www.pingdom.com/)"
        }
      }
    },
    "hostname" : "s7.mydomain.com",
    "status" : "up",
    "lasterrortime" : 1293143467,
    "lasttesttime" : 1294064823
  }
}

这是我的Perl代码,应该打印网址:

my $decoded_info = decode_json($json) or die "Failed to decode!\n";
foreach my $check( $decoded_info->{check}) {
  print "$decoded_info->{$check}->{type}->{http}->{url}\n";
}

我已经阅读了Perl参考和教程,但它仍然无法正常工作。

2 个答案:

答案 0 :(得分:1)

你想要

$decoded_info->{check}->{type}->{http}->{url}    # ok

$check的值为

$decoded_info->{check};

所以你应该使用

$check->{type}->{http}->{url}                    # ok

而不是

$decoded_info->{$check}->{type}->{http}->{url}   # BAD

顺便说一下,

my $check = $decoded_info->{check};
...

简单
foreach my $check( $decoded_info->{check}) {
    ...
}

答案 1 :(得分:0)

检查中只有一个项目。

{
  "check" : {                     // here
    "id" : 85975,
    "name" : "My check 7",
    "resolution" : 1,
    "sendtoemail" : false,
    "sendtosms" : false,
    "sendtotwitter" : false,
    "sendtoiphone" : false,
    "sendnotificationwhendown" : 0,
    "notifyagainevery" : 0,
    "notifywhenbackup" : false,
    "created" : 1240394682,
    "type" : {
      "http" : {
        "url" : "/",
        "port" : 80,
        "requestheaders" : {
          "User-Agent" : "Pingdom.com_bot_version_1.4_(http://www.pingdom.com/)"
        }
      }
    },
    "hostname" : "s7.mydomain.com",
    "status" : "up",
    "lasterrortime" : 1293143467,
    "lasttesttime" : 1294064823
  }
}

没有理由迭代任何事情。您可以摆脱foreach循环,只需使用字符串check作为您的哈希键。

#                      vvvvv
print "$decoded_info->{check}->{type}->{http}->{url}\n";