【发布时间】:2012-04-25 01:58:28
【问题描述】:
尝试通过cmd 运行我的perl 脚本时,提示我的json 字符串返回[] 我已阅读其他帖子,并将我的数据库修复为utf8,但错误仍然存在。我尝试了两种不同的方式来编码我的 perl 字符串,第一种是$json = encode_json @temp_array,它返回这个错误hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this 但是,当我使用这条线$json_text = $json->encode(@temp_array) 我只是得到[]
这是我的 perl:
my $json_text;
my $json = JSON->new->utf8;
my @temp_array =[];
my $temp_array;
while (@data = $query_handle->fetchrow_array())
{
my %json_hash = ();
my $hash_ref;
%json_hash = (
"User ID" => $data[0],
"Status" => $data[1],
"Last Password Reset" => $data[2],
"Reset Needed" => $data[3]
);
$hash_ref = \%json_hash;
push (@temp_array, $hash_ref);
}
print $json = encode_json @temp_array . "\n"; #encode with error
print $json_text = $json->encode(@temp_array) . "\n"; #encode with []
print $cgi->header(-type => "application/json", -charset => "utf-8");
print $json_text; #Prints []
所以在我自己的测试中,通过cmd prompt 我知道while 正在正确地从我的数据库中检索数据并正在构建一个哈希,我认为这是正确的。
是不是我将哈希引用推送到数组而不是哈希本身?一旦我正确构建了这个字符串,我将通过 jquery 将其调用为 html
谢谢。
【问题讨论】: