나는 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);
내가 현재 그게 내가 $의 콘텐츠에서 원하는 값을 잡고 무엇을 내 코드입니다. 내가 필요한 첫 번째 결과는 첫 번째 실행 후 루프를 죽이는 이유입니다. 루프없이 이것을 시도 할 때마다 "해시 참조가 아닙니다."가 표시됩니다. 그래서 내가 알아야 할 것은 $ 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;
}
* 루프없이 * 시도하면 어떻게 생겼습니까? 이 같은? '$ json_text - {factMap} { 'T! T'} {행렬} [0] {dataCells} [1] {레이블} ) ' –
아니요, 그러나 당신이 제공 한 것은 완벽하게 작동했습니다. 감사! 나는 그것이 가능한 한 빨리 당신의 대답을 받아 들일 것이다, 나는 초록색 수표가 나타나지 않기 때문에 새로운 사용자 또는 무언가를위한 한계가 있다고 생각한다. – hypnotoad
Jim 님은 답변을 아직 게시하지 않았습니다. 코멘트 섹션입니다. – TLP