2013-08-16 144 views
1

我想使用Zabbix JSON API在我们的IT商店中自动化一些监控的东西。 我想使用这里描述的graph.create方法:https://www.zabbix.com/documentation/2.2/manual/api/reference/graph/create哈希表在阵列....和JSON

我正在与gitems数组挣扎。它必须包含散列表(图中每个项目一个),每个都有“itemid”和“color”行。

这是我的代码部分:

#i get a list of itemids in $items 
[email protected]("C04000", "800000", "191970", "3EB489", [...]) 
[email protected]{} 
     [email protected]() #an array of hash tables... 

     $c=0 
     foreach ($itemid in $items.result) { 
      [email protected]{} 
      $graphmember.add("itemid", $itemid) 
      $graphmember.add("color", $colours[$c]) 
      $gitems += $graphmember 
      $c += 1 
     } 
     $params.add("gitems", $gitems) 

     #construct the JSON object 
     $objgraph = (New-Object PSObject | Add-Member -PassThru NoteProperty jsonrpc '2.0' | 
     Add-Member -PassThru NoteProperty method 'graph.create' | 
     Add-Member -PassThru NoteProperty params $params | 
     Add-Member -PassThru NoteProperty auth $session.result | 
     Add-Member -PassThru NoteProperty id '2') | ConvertTo-Json 

     return $objgraph 

,当所谓的回报是:

{ 
    "jsonrpc": "2.0", 
    "method": "graph.create", 
    "params": { 
        "gitems": [ 
            "System.Collections.Hashtable", 
            "System.Collections.Hashtable", 
            "System.Collections.Hashtable", 
            "System.Collections.Hashtable", 
            "System.Collections.Hashtable" 
           ] 
       }, 
    "auth": "dc50acf4c337e5430c00936f998f74da", 
    "id": "2" 
} 

,所以我得到5行,这是基于我所提供的参数正确的号码,但似乎convertto-json不喜欢我的对象...不知道为什么。

我在一个阵列的事情是不知道的哈希表,所以我做了一个测试,它似乎工作:

[email protected]() 
[email protected]{} 
$1.add("itemid","123") 
$i1.add("color","blue") 
$gitems += $i1 
[email protected]{} 
$i2.add("itemid","567") 
$i2.add("color","yellow") 
$gitems += $i2 

$gitems 

Name       Value 
----       ----- 
color       bleu 
itemid       123 
color       yellow 
itemid       567 

感谢想法的人!

+0

似乎类似于[这里](http://stackoverflow.com/questions/17929494/powershell-v3-convertto -json-与嵌入的散列表) –

回答

2

depth参数指定包含的对象在JSON表示中包含的级别。默认值是2。如果你指定值3,JSON将成功创建:

$objgraph = (New-Object PSObject | Add-Member -PassThru NoteProperty jsonrpc '2.0' | 
Add-Member -PassThru NoteProperty method 'graph.create' | 
Add-Member -PassThru NoteProperty params $params | 
Add-Member -PassThru NoteProperty auth $session.result | 
Add-Member -PassThru NoteProperty id '2') | ConvertTo-Json -depth 3