2

我正在使用肥皂制作网络服务。我一直试图弄清楚使用 perl 将哈希转换为 JSON。现在我正在客户端对其进行测试,并且我正在尝试打印出 JSON - 所以我不确定我的问题是出在服务器端还是客户端。

这是我的服务器端代码。

 #create hash
 my %hash_to_encode = (
    weigh => $weight,
    price => $price,
    unit  => $unit,
    picture=> $picture,
    amount => $amount,
    dimensions => $dimensions,
    country => $country,
    description => $description,
    name => $name,
    category => $category,
    comment => $comment,
    expires => $expires
  );

 my $json_string = JSON->new->utf8->encode(%hash_to_encode);

return $json_string;

My client side code:
#! /usr/bin/perl -w
use CGI qw(:standard);
use SOAP::Lite;
use JSON;
print "Content-type: text/html\n\n";

my $exhibitID = '13';
my $username = 'us43';
my $password = 'green4356';
my $link = 'its a secret :P';
my $namespace = 'also a sewcret';
#The soap call works (i have other functions and they work as expected so dont worry about this part.
my $mySoap = SOAP::Lite
 -> uri($link)
 -> proxy($namespace);

#This is where the error lies.
my $result = $mySoap->status($exhibitID, $username, $password)->result;
print "Json String: $result";

#my $jsonString= JSON->new->utf8->decode($result);
#print "Status is: $jsonString";

print "<p> Finished status</p>";

谢谢您的帮助 :)

4

1 回答 1

3
my $json_string = JSON->new->utf8->encode(\%hash_to_encode);

您需要提供哈希引用,而不仅仅是编码函数的哈希。

为此,可以像我上面所做的那样添加反斜杠,或者如下更改定义(尽管只做两者之一......)

my $hash_to_encode = {
weigh => $weight,
price => $price,
unit  => $unit,
picture=> $picture,
amount => $amount,
dimensions => $dimensions,
country => $country,
description => $description,
name => $name,
category => $category,
comment => $comment,
expires => $expires
};
于 2012-04-07T05:46:15.257 回答