2

せっけんを使ったWebサービスの作成に取り組んでいます。私は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);

エンコード関数へのハッシュだけでなく、ハッシュ参照を提供する必要があります。

これを行うには、上記のように円記号を追加するか、以下のように定義を変更します(ただし、2つのうちの1つのみを実行してください...)

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 に答える