【问题标题】:SOAP create JSON from hash and decode from clientSOAP 从散列创建 JSON 并从客户端解码
【发布时间】:2012-04-20 13:52:19
【问题描述】:

我正在使用肥皂制作网络服务。我一直试图弄清楚使用 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>";

感谢您的帮助:)

【问题讨论】:

    标签: json perl soap hash


    【解决方案1】:
    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
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-20
      • 1970-01-01
      • 2018-05-27
      • 2019-02-28
      相关资源
      最近更新 更多