這是我的代碼爲什麼我得到這個錯誤?
#!/usr/bin/perl -T
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use CGI qw(:standard);
use JSON;
use utf8;
use strict;
use warnings;
# ... ;
my $cgi = CGI->new;
$cgi->charset('UTF-8');
my @owners = map { s/\s*//g; $_ } split ",", $cgi->param('owner');
my @users = map { s/\s*//g; $_ } split ",", $cgi->param('users');
my $json = JSON->new;
$json = $json->utf8;
my %user_result =();
foreach my $u (@users) {
$user_result{$u} = $db1->{$u}{displayName};
}
my %owner_result =();
foreach my $o (@owners) {
$owner_result{$o} = $db2{$o};
}
$json->{"users"} = $json->encode(\%user_result);
$json->{"owners"} = $json->encode(\%owner_result);
$json_string = to_json($json);
print $cgi->header(-type => "application/json", -charset => "utf-8");
print $json_string;
和這些行
$json->{"users"} = $json->encode(\%user_result);
$json->{"owners"} = $json->encode(\%owner_result);
給出了錯誤
Not a HASH reference
爲什麼我明白了嗎?
這怎麼解決?
「你應該使用新的散列引用來保存這些數據並傳遞給JSON」。這正是我想要做的。 =)我該怎麼做? – 2011-05-16 16:27:31
'my $ data; \ n $ data - > {users} = $ json-> encode(\%user_result); \ n $ data - > {owners} = $ json-> encode(\%owner_result); \ n $ json_string = to_json($ data);',單程 – mob 2011-05-16 16:34:12
非常感謝!它立即工作=) – 2011-05-16 17:21:39