我正在执行JSON解析以及我获取的所有JSON文件的最终哈希值。被调用的函数是传递我需要打印的索引条目。如果我尝试将变量原样放入..它会因错误而死亡。 注释掉的行是我想要的...使用此函数的原因是传递不同的值并获得我需要的任何组合。
&Function('{value}{Agent}{macName}')
sub Function(){
my $key = $_[0];
my $URL = "www.xyz.com";
my $json = get( $URL );
my $decoded_json = decode_json( $json );
for ( @{$decoded_json->{value}} ) {
#print $_->{value}{Agent}{macName}."\n";
print $_->$key."\n";
}
}
Can't call method "{value}{Agent}{macName}" on unblessed reference at Json_Decoder.pl line 34.
如何访问此条目? print $ _-> {value} {Agent} {macName} ...使用此函数的'{value} {Agent} {macName}'参数。
答案 0 :(得分:2)
使用Data::Diver,可以将散列键的“路径”指定为列表:
julia> wasc = "APP #2 bias voltage [V]"
"APP #2 bias voltage [V]"
julia> w16 = utf16("APP #2 bias voltage [V]")
"APP #2 bias voltage [V]"
julia> w32 = utf32("APP #2 bias voltage [V]")
"APP #2 bias voltage [V]"
julia> wsub = SubString(wasc,1,length(wasc))
"APP #2 bias voltage [V]"
julia> wvec = ["APP #2 bias voltage [V]"]
1-element Array{ASCIIString,1}:
"APP #2 bias voltage [V]"
julia> wvec == wsub
false
julia> wvec[1] == wsub
true
julia> wvec[1] == w16
true
julia> wvec[1] == w32
true
julia> typeof(w32)
UTF32String
另请注意,您不需要#!/usr/bin/perl
use warnings;
use strict;
use Data::Diver qw{ Dive };
sub Function {
my $key = shift;
my $decoded_json = { value => { Agent => { macName => 'name' } } };
my @path = split /\}\{/, $key;
$path[-1] =~ s/\}$//;
$path[0] =~ s/^\{//;
print Dive($decoded_json, @path), "\n";
}
Function('{value}{Agent}{macName}');
来调用该函数,并且您的代码中的空原型&
是错误的(但是因为函数在“声明”之前被调用而被忽略)。有关详细信息,请参阅perlsub。