2016-05-31 36 views
0

嗨,夥計們,我想問問題,但我想知道是什麼過程,以從下面的json輸出中刪除'name'值,我知道如何獲取單個名稱索引name = [groups['groups'][0]['name'] for groups in result]但我如何得到這兩個名字從json輸出中提取多個字段

from pyzabbix import ZabbixAPI 

import json 

z = ZabbixAPI(url='https://zabbix.server,user='xxxx',password='xxxx') 
result = z.host.get(output='groupid',selectGroups='extend', filter={'host' : 'mailpphfprd01'}) 


name = [groups['groups'] for groups in result] 

name 

JSON輸出:

[[{u'flags': u'0', 
    u'groupid': u'110', 
    u'internal': u'0', 
    u'name': u'- GSOC - Systems'}, 
    {u'flags': u'0', 
    u'groupid': u'313', 
    u'internal': u'0', 
    u'name': u'Applications - Email Digest'}]] 

需要的輸出

[u'- GSOC - Systems' u'Applications - Email Digest'] 

回答

1

你應該能夠做到這一點的一個list-comprehension

json_output = [ 
    [ 
    { 
     "internal": "0", 
     "flags": "0", 
     "groupid": "110", 
     "name": "- GSOC - Systems" 
    }, 
    { 
     "internal": "0", 
     "flags": "0", 
     "groupid": "313", 
     "name": "Applications - Email Digest" 
    } 
    ] 
] 

names = [d['name'] for d in json_output[0]] 

print(names) # -> [u'- GSOC - Systems', u'Applications - Email Digest'] 
+0

'json_output [0]'爲什麼放在這裏的指數是代表內部[] – ryans11

+0

JSON的輸出列表列表字典,所以我只是硬編碼外部列表中第一個條目的索引 - 換句話說,'json_output [0]'是該最外層列表的第一個(也是唯一)條目。 – martineau