为什么我会收到此错误?
这是我的代码
#!/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
为什么我会得到这个错误?
如何解决这个问题?
This is my code
#!/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;
and these lines
$json->{"users"} = $json->encode(\%user_result);
$json->{"owners"} = $json->encode(\%owner_result);
gives the error
Not a HASH reference
Why do I get that?
How can that be fixed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
JSON 对象(至少在 XS 版本中,见下文)只是一个 SCALAR 引用,因此您无法对其执行哈希引用操作。实际上,您遇到的大多数 Perl 对象都是哈希引用,但情况并非总是如此。
我不确定您想要通过使用 JSON 来编码 JSON 对象来完成什么。您需要对 JSON 对象的内部进行编码吗?或者您只需要序列化用户和所有者数据?在后一种情况下,您应该只使用新的哈希引用来保存该数据并传递给 JSON。如果您确实需要对 JSON 对象进行编码,那么使用
JSON::PP
(JSON 模块的“Pure Perl”变体)可能会更好,它确实使用了哈希引用。A
JSON
object (at least in the XS version, see below) is just a SCALAR reference, so you can't perform a hash reference operation on it. In practice, most of the Perl objects you encounter will be hash references, but this will not always be the case.I'm not sure what you are trying to accomplish by using JSON to encode a JSON object. Do you need to encode the internals of the JSON object? Or do you just need to serialize the user and owner data? In the latter case, you should just use a new hash reference to hold that data and pass to JSON. If you really do require an encoding of the JSON object, you might have some better luck using
JSON::PP
(the "Pure Perl" variant of the JSON module), which does use a hash reference.在我看来,
$json = $json->utf8;
正在用标量($json->utf8 的结果)替换 $json 哈希引用。在分配给 $json->{...} 的行之前,使用 Data::Dumper 模块中的 Dumper 查看其中的内容。
Looks to me that
$json = $json->utf8;
is replacing the $json hash ref with a scalar, the result of $json->utf8.Before the lines that assign to $json->{...}, use Dumper from the Data::Dumper module to see what is in it.
因为在你的例子中 $json 是编码器本身,它是对 SCALAR 的引用。尝试使用不同的变量来保存结果。像这样的东西
Because $json in your case is encoder itself, which is reference to SCALAR. Try to have different variable to hold your result. Something like
你的大问题是
$json
是 JSON 编码器对象,而不是要编码的数据结构。您应该制作一个单独的数据结构。您的另一个问题是您正在尝试对 JSON 进行双重编码。此代码:
将创建一个 JSON 字符串,解码后,将为您提供带有两个键的哈希值。每个键的值都是一个包含 JSON 编码哈希的字符串。每个值都是一个哈希值更有意义。
所以,试试这个:
在这里,我使用匿名哈希引用,因为不需要为要编码的哈希提供名称。我们只使用一次。
Your big problem is that
$json
is the JSON encoder object, not the data structure to be encoded. You should be making a separate data structure.Your other problem is that you're trying to double encode your JSON. This code:
would create a JSON string that, when decoded, would give you a hash with two keys. The value of each key would be a string containing a JSON-encoded hash. It makes more sense for each value to be a hash.
So, try this:
Here, I'm using an anonymous hashref, because there's no need to give the hash to be encoded a name. We're only using it once.