Perl Newbie - 从阵列打印价值

时间:2014-02-17 22:20:58

标签: perl

我是perl的新手。我试图从一些JSON输出中获取一个字符串。我的代码可以使用,但似乎有一种更聪明的方法可以做到这一点。以下是JSON的示例

$VAR1 = '{
    "hasDetailRows" : true,
     "reportMetadata" : {
     "reportFormat" : "TABULAR",
    "detailColumns" : [ "SUBJECT", "COMMENT_CREATED_DATE", "CASE_COMMENT_CREATED_BY" ],
    "reportBooleanFilter" : null,
    "reportFilters" : [ {
      "column" : "CASE_COMMENT_CREATED_BY",
      "operator" : "equals",
      "value" : "Username"
    } ],
    "aggregates" : [ "RowCount" ],
    "groupingsDown" : [ ],
    "groupingsAcross" : [ ],
    "developerName" : "My_Comments",
    "reportType" : {
      "type" : "CaseList",
      "label" : "Cases"
    },
    "name" : "My Comments",
    "id" : "REDCATED",
    "currency" : null
  },
      "factMap" : {
       "T!T" : {
        "rows" : [ {
        "dataCells" : [ {
          "value" : "ID",
          "label" : "Description"
        }, {
          "value" : "2014-02-17T22:01:17Z",
          "label" : "2/17/2014 4:01 PM"
        }, {
          "value" : "USER ID",
          "label" : "User Name"
        } ]
      }`

我需要的是带有时间戳的标签。

这是我的代码

#!/usr/bin/perl
use warnings;
use strict;
use WWW::Mechanize;
use WWW::Salesforce;
use Data::Dumper;
use JSON -support_by_pp;
use 5.010;
use Date::Parse;


my $mech = WWW::Mechanize->new();
$mech->agent('Mozilla/5.0');

# Authenticate first via SOAP interface to get a session ID:
my $sforce = eval { WWW::Salesforce->login(

                    username => 'REDACTED',
                    password => 'REDACTED' ); };
die "Could not login to SFDC: $@" if $@;

 # Get the session ID:
my $hdr = $sforce->get_session_header();
my $sid = ${$hdr->{_value}->[0]}->{_value}->[0];

#Our request
$mech->add_header( "Authorization" => "OAuth $sid" );
$mech->add_header( "X-PrettyPrint" => '1' );
$mech->get("SOME URL THAT RETURNS JSON");
my $content =  $mech->content;


my $json = new JSON;
my $json_text =    $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode($content);

以下是我目前从$ content中获取我想要的价值的内容。我需要的只是第一个结果,这就是我在第一次运行后杀死循环的原因。每次我在没有循环的情况下尝试过此操作时,我会得到“非HASH参考”。所以我需要知道的是打印出$ timestamp值的正确语法。

foreach my $field(@{$json_text->{factMap}->{'T!T'}->{rows}}){
  my $now = time();

my $timestamp = str2time($field->{dataCells}[1]->{'label'});
my $diff = int (($now - $timestamp)/60);
my $current = getLoggingTime();
print  $current . " \t";
say "Time since last activity: " . $diff . " minutes!" ;
  last;
 }




sub getLoggingTime {

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
my $nice_timestamp = sprintf ( "%04d-%02d-%02d %02d:%02d:%02d",
                               $year+1900,$mon+1,$mday,$hour,$min,$sec);
return $nice_timestamp; 
} 

1 个答案:

答案 0 :(得分:0)

我不知道你的无循环版本是什么样的,但这应该有效:

my $timestamp = str2time(
    $json_text->{factMap}{'T!T'}{rows}[0]{dataCells}[1]{label}
);

目前,您正在循环遍历

引用的数组元素
$json_text->{factMap}{'T!T'}{rows}

...并在第一个条目停止

$json_text->{factMap}{'T!T'}{rows}[0]

...并取消引用该内容

$json_text->{factMap}{'T!T'}{rows}[0]{dataCells}[1]{label}