如何解析以HASH格式的数据的SOAP响应的输出?有关详情,请参见下面的代码

时间:2019-05-12 13:19:10

标签: perl parsing soap hash

#perl!
use warnings;
use strict;
use XML::Compile::SOAP;
use XML::Compile::SOAP11;
use XML::Compile::WSDL11;
use XML::Compile::Transport::SOAPHTTP;
use Data::Dumper;
##my other variables pointing to wsdl and xsd files.
my $url="http://myhost.com:9080/imws/services/ImpactManager/";
my %inargs_connect = (
    "userName"  => "xxxx",
    "password" => "xxxxxxxxxx",
    "imname"  => "yyyyyyyyyyy",
    "bufferType"  => "abcd"
);

my $wsdl = XML::Compile::WSDL11->new;
$wsdl->addWSDL($wsdl_file);
$wsdl->compileCalls(address =>$url);
my ($answer, $trace) = $wsdl->call( 'Connect', %inargs_connect);
print ($answer);

上面的代码正在打印: 哈希(0x47f8b28)

如果我使用转储程序,在最后一个打印语句中

,我得到以下响应。 print Dumper($answer);

$VAR1 = {
          'outargs' => {
                         'connectionId' => '1557666855346'
                       }
        };

如何解析所需的值,例如, 我需要能够轻松访问'connectionId'和'1557666855346'吗?

任何想法都欢迎。提前致谢。 谢谢, 考希克(KM)。

3 个答案:

答案 0 :(得分:1)

$answer似乎是哈希引用,因此您可以使用常规的解引用技术访问数据:

my $conn_id = $answer->{outargs}{connectionId};
print "$conn_id\n";

输出:

1557666855346

答案 1 :(得分:0)

my %fhash = %{$answer};
my $key = "";
foreach $key (keys %fhash)
{
print "keys are\n \"$key\" its value is (${$answer}{$key}) \n";

    foreach my $key2 (keys %${$answer}{$key})
    {
    print "keys of(${$answer}{$key})\n \"$key2\"\n";
    }
}

不起作用,并引发如下错误: 现在,CreateEvent.pl第64行禁止使用标量上的实验键。 在创建时,键1的arg 1类型必须是哈希或数组(不是键/值哈希片) Event.pl第64行,位于“}

附近

有人可以改正吗?

答案 2 :(得分:0)

循环遍历answear = {outargs => {key1,key2}}

中的所有键
# This should remove the error of "Experimental keys on scalar is now forbidden" by wrapping the hashRef in %{}
for ( keys %{$answear->{outargs}}) {
    # $_ here will be the key and if you want to access the value use $answear->{outargs}->{$_}
    print "key: $_, value: $answear->{outargs}->{$_}\n";
}

输出:

key: connectionId, value: 1557666855346

希望这对您有帮助!