Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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/9.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
Json 尝试在哈希内的数组中循环时未收到哈希引用错误_Json_Perl_Hash - Fatal编程技术网

Json 尝试在哈希内的数组中循环时未收到哈希引用错误

Json 尝试在哈希内的数组中循环时未收到哈希引用错误,json,perl,hash,Json,Perl,Hash,我一直在尝试制作一个reddit机器人,它从给定的子reddit数组中获取版主列表。到目前为止,我只尝试使用RedditAPI检索一个mod列表(JSON格式)。我已经使用JSON perl模块对其进行了解码,现在我正试图通过它来构建一个mod列表。这是我的密码: use strict; use warnings; use LWP; use JSON; use Data::Dumper; my $ua = LWP::UserAgent->new('RedditBot/ModScraper

我一直在尝试制作一个reddit机器人,它从给定的子reddit数组中获取版主列表。到目前为止,我只尝试使用RedditAPI检索一个mod列表(JSON格式)。我已经使用JSON perl模块对其进行了解码,现在我正试图通过它来构建一个mod列表。这是我的密码:

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

my $ua = LWP::UserAgent->new('RedditBot/ModScraper');
my $req = HTTP::Request->new(GET => 'http://www.reddit.com/r/funny/about/moderators.json');
my $res = $ua->request($req);


if($res->is_success){
    my $djson = decode_json($res->content);
    my @datalist = $djson->{'data'}{'children'};
    my @names;
    for(my $i=0;$i<@datalist;$i++){
            push(@names, $datalist[$i]->{'name'});
    }
    print Dumper @names;

}
else{
    print $res->status_line, "\n";
}
我的错误是:

Not a HASH reference at modscraper.pl line 15.
我知道这可能是一件小事,但我是一个新的Perl程序员,以前没有做过很多JSON/网络工作。谢谢你的阅读


编辑:忘了提到,第15行是for循环,转储的数组/散列是@datalist。

因为您给出的转储的散列/数组值是数组引用,所以datalist变量应该定义为数组引用,所以该变量的代码行应该改为:

my $datalist = $djson->{'data'}{'children'};
此外,我们必须更改for循环代码,因为现在datalist是一个数组ref,所以您可以将for循环代码更改为:

for(my $i=0;$i<@$datalist;$i++){
    push(@names, $datalist->[$i]->{'name'});
}
for(my$i=0;$i[$i]->{'name'});
}

是否$datalist[$i]是散列?try$datalist[$i]{'name'}$datalist[$i]是一个散列,但是错误出现在for循环的第15行。也就是说,我尝试了$datalist[$I]{'name'},但什么也没发生。$datalist[$I]是一个数组,而不是散列。如果您想要散列,您需要使用“%”而不是“@”,谢谢您的回答,这澄清了一切!我刚刚再次运行了代码并查找了一些关于数组引用的信息,向量中填充了我想要的信息。
for(my $i=0;$i<@$datalist;$i++){
    push(@names, $datalist->[$i]->{'name'});
}