我一直在尝试创建一个reddit bot,它从给定的subreddits数组中删除主持人列表。到目前为止,我只尝试使用reddit API来检索一个单一的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";
}
这是Dumped哈希/数组:
$VAR1 = [
{
'mod_permissions' => [
'all'
],
'date' => '1201242956',
'name' => 'illuminatedwax',
'id' => 't2_14nvj'
},
{
'mod_permissions' => [
'all'
],
'date' => '1282272786',
'name' => 'krispykrackers',
'id' => 't2_35gvu'
},
{
'mod_permissions' => [
'all'
],
'date' => '1306697593',
'name' => 'doug3465',
'id' => 't2_4d9s0'
},
{
'mod_permissions' => [
'all'
],
'date' => '1306884548',
'name' => 'Kylde',
'id' => 't2_q22x'
},
{
'mod_permissions' => [
'all'
],
'date' => '1318239301',
'name' => 'GuitarFreak027',
'id' => 't2_3uh1q'
},
{
'mod_permissions' => [
'all'
],
'date' => '1328330491',
'name' => 'sodypop',
'id' => 't2_39hzo'
},
{
'mod_permissions' => [
'all'
],
'date' => '1334668866',
'name' => 'brownboy13',
'id' => 't2_4itnj'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1338375544',
'name' => 'neptath',
'id' => 't2_42sls'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1338375575',
'name' => 'kabuliwallah',
'id' => 't2_3wqp9'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1338375584',
'name' => 'poop_monster',
'id' => 't2_45a14'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1342052024',
'name' => 'WellEndowedMod',
'id' => 't2_66grn'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1351424249',
'name' => 'complex_reduction',
'id' => 't2_4y36s'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1351427857',
'name' => 'anions',
'id' => 't2_33ggu'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1363446597',
'name' => 'Livyka',
'id' => 't2_5p6ae'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1363450026',
'name' => 'Lord_Nuke',
'id' => 't2_4fvxr'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1363450241',
'name' => 'redtaboo',
'id' => 't2_3belm'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1363453699',
'name' => 'Knowltey',
'id' => 't2_3aadp'
},
{
'mod_permissions' => [
'wiki',
'posts',
'flair'
],
'date' => '1401789143',
'name' => 'AutoModerator',
'id' => 't2_6l4z3'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1401789148',
'name' => 'thejellydude',
'id' => 't2_3g59f'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'config',
'flair'
],
'date' => '1401789172',
'name' => 'DuckDragon',
'id' => 't2_52snu'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1401789188',
'name' => 'Kensin',
'id' => 't2_4nbrl'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1401789200',
'name' => 'cahman',
'id' => 't2_6y7ld'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1401789214',
'name' => 'topodan',
'id' => 't2_8nisz'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1401789225',
'name' => 'ADHD_orc',
'id' => 't2_5dwik'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1401789236',
'name' => 'all_my_rage',
'id' => 't2_6z81j'
},
{
'mod_permissions' => [
'mail'
],
'date' => '1401789251',
'name' => 'funny_mod',
'id' => 't2_5qz0k'
},
{
'mod_permissions' => [
'wiki',
'posts',
'mail',
'flair'
],
'date' => '1401789269',
'name' => 'benologist',
'id' => 't2_358ts'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1401789281',
'name' => 'dizzie131',
'id' => 't2_bb3q5'
},
{
'mod_permissions' => [
'wiki',
'posts',
'access',
'mail',
'flair'
],
'date' => '1401789298',
'name' => 'KimJongUnchained',
'id' => 't2_b9c4n'
}
];
我的错误是:
Not a HASH reference at modscraper.pl line 15.
我知道这可能是微不足道的,但我是一个非常新的Perl程序员,之前没有做过多JSON / Networking工作。谢谢你的阅读。
编辑:忘记提及,第15行是for循环,转储数组/散列是@datalist。
答案 0 :(得分:2)
由于您提供的转储哈希/数组值是数组引用,因此应将datalist变量定义为数组引用,因此应将其代码行更改为:
my $datalist = $djson->{'data'}{'children'};
另外,我们必须更改for循环代码,因为现在datalist是一个数组引用,所以你可以将for循环代码更改为:
for(my $i=0;$i<@$datalist;$i++){
push(@names, $datalist->[$i]->{'name'});
}