Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Perl中,如何仅打印通过json提取的值?_Json_Perl - Fatal编程技术网

在Perl中,如何仅打印通过json提取的值?

在Perl中,如何仅打印通过json提取的值?,json,perl,Json,Perl,我有一个包含内容的json文件。我正在使用 提取json。我想访问属性“d”的值 sub-extract\u json{ 我的$file=shift; 本地$/; 打开我的$fh,“尝试以下操作 use warnings; use strict; use JSON::XS; my $targetfile = extract_json('file.json'); my $object = JSON::XS->new->utf8->decode($targetfile); my

我有一个包含内容的json文件。我正在使用 提取json。我想访问属性“d”的值

sub-extract\u json{
我的$file=shift;
本地$/;
打开我的$fh,“尝试以下操作

use warnings;
use strict;
use JSON::XS;
my $targetfile = extract_json('file.json');
my $object = JSON::XS->new->utf8->decode($targetfile);

my $flat_hash = 
{
    'var'=> $object->{'a'}{'b'}{'c'}{'d'}
};


sub extract_json{
    my $file = shift;
    local $/;
    open my $fh, "<", "$file";
    my $json = <$fh>;
    return $json;
}

foreach my $key (keys %{$flat_hash}) #Dereferencing hash ref
{

    foreach (@{$$flat_hash{$key}})  #Iterating loop for array ref
    {
        print "$key => $_\n";
    }

}
使用警告;
严格使用;
使用JSON::XS;
my$targetfile=extract_json('file.json');
我的$object=JSON::XS->new->utf8->decode($targetfile);
我的$flat\u散列=
{
'var'=>$object->{'a'}{'b'}{'c'}{'d'}
};
子摘录{
我的$file=shift;
本地$/;
打开我的$fh,“$\n”;
}
}
尝试以下代码:

use strict;
use warnings;
use JSON;
use Data::Dumper;

sub extract_json{
        my $file =shift;
        open(my $fh, "<:encoding(UTF-8)" , $file) || die "couldn't open $file";
        local $/;
        my $json=<$fh>;
return $json;
}

my $targetfile = extract_json('example.json');
my $object = JSON->new;
my $decoded= $object->decode($targetfile);

my $flat_hash ={
               'var' => $decoded->{a}{b}{c}->@{d},
     };
 print Dumper $flat_hash;
使用严格;
使用警告;
使用JSON;
使用数据::转储程序;
子摘录{
我的$file=shift;

打开(我的$fh,”上面的代码产生了什么?它给了我一个错误,说值不是标量或未定义。我需要取消引用才能在标量上下文中获得它吗?我是否可以将json文件中的值更改为d1:31和d2:45,然后提取这两个值,并在perl中以某种方式连接它们以获得所需的输出?您可以尝试
Dump
ing$obJET,也许结构不是你认为的那样,看到原始的JSON真的会有帮助。没有它,我们只是猜测。
use strict;
use warnings;
use JSON;
use Data::Dumper;

sub extract_json{
        my $file =shift;
        open(my $fh, "<:encoding(UTF-8)" , $file) || die "couldn't open $file";
        local $/;
        my $json=<$fh>;
return $json;
}

my $targetfile = extract_json('example.json');
my $object = JSON->new;
my $decoded= $object->decode($targetfile);

my $flat_hash ={
               'var' => $decoded->{a}{b}{c}->@{d},
     };
 print Dumper $flat_hash;