2014-02-17 37 views
0

我是perl的新手。我想從一些JSON輸出中獲取一個字符串。我的代碼如下,但似乎有一個更聰明的方法來做到這一點。這裏是一個JSON樣本Perl新手 - 從數組中打印值

$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: [email protected]" if [email protected]; 

# 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); 

這裏是我目前唯一抓住,我從$內容所需的值。我需要的只是第一個結果,這就是爲什麼我在第一次運行後殺死循環。每次我嘗試了這個沒有循環,我得到「不是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; 
} 
+0

當您嘗試此操作*沒有*循環時,它是什麼樣子的?像這樣? 'my $ timestamp = str2time( $ json_text - > {factMap} {'T!T'} {rows} [0] {dataCells} [1] {label} );' –

+0

否我有別的,但你所提供的完美工作。謝謝!我會盡快接受你的回答,我想對新用戶或其他事情有限制,因爲綠色檢查並沒有顯示出來。 – hypnotoad

+0

Jim還沒有發佈任何答案,這是評論版塊。 – TLP

回答

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} 
+0

完美的作品!謝謝! – hypnotoad