0
我想通過使用JSON RPC模塊調用zabbix api host.update。 $ json變量Perl - 在perl中爲zabbix host.update api創建json對象
$json = {
jsonrpc => '2.0',
method => 'host.update',
params => {
hostid => "$host_id", # global variable from the first function host.get
groups => [
{ groupid => "$arg1" },
{ groupid => "$arg2" },
{ groupid => "$arg3" },
],
},
id => 2,
auth => "$authID",
};
$response = $client->call($url, $json);
這工作正常。但問題是我們有一個動態的groupid列表。它不總是3個組。
因此,我爲groupids創建了一個散列數組,併爲散列變量保存了其他信息。
如
# @gid is an array of group ids
my @groups; # this array will hold records of hash ie array of hash records
foreach my $id (@gid) {
push(@groups, { groupid => $id });
# construct array of hash records of groupids
}
my $groupjson = encode_json(\@groups);
my %data = (
jsonrpc => '2.0',
method => 'host.update',
params => { hostid => "@hid", groups => "$groupjson" },
id => 1,
auth => "$authID"
);
my $datajson = encode_json \%data;
$response = $client->call($api_url, $datajson);
當我運行上面的代碼,我得到了「羣組」
任何一個可以請幫助我的錯誤「不是一個散列引用」?
怎麼樣'groups => \ @ groups'?而且你不必強制串化,即。 '「$ authID」','$ authID'就足夠了。 – 2014-09-24 13:48:27
groups => \ @groups OR groups => \ @groupjson? – 2014-09-24 13:53:16
只是@ @組。你可以一次完成所有的json編碼。 – 2014-09-24 13:54:20