2012-04-07 3 views
2

私はSOAPを使用してWebサービスを作成しています。私はperlを使ってJSONにハッシュを変換しようとしています。今私はクライアント側でテストしています。JSONをプリントアウトしようとしています。そのため、問題がサーバー側かクライアント側にあるかわかりません。SOAPは、ハッシュから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>"; 

助けてくれてありがとう:)

答えて

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 
}; 
(...のみものの2のいずれかを実行)

これを行うにはどちらか、私が上に行ったようにバックスラッシュを追加したり、以下のように定義を変更

関連する問題