2016-11-16 62 views
1

JSON鍵我有這樣的JSON對象上檢索搜索子值

{ 
    '2807281f81481ffbd4183da0003dfe1820343ff6': { 
    'macaddr': '64:a3:cb:ba:5c:76', 
    'parameters': [ 
     '1', 
     '121', 
     '3', 
     '6', 
     '15', 
     '119', 
     '252' 
    ], 
    'ip': '192.168.1.120', 
    'vendor_id': 'MSFT 5.0', 
    'HOSTID': '2807281f81481ffbd4183da0003dfe1820343ff6', 
    'time': 1479309949.7988095, 
    'os': 'Apple iPhone', 
    'hostname': 'Alles-iPhone' 
    } 
} 

我想實現的是檢索上搜索特定的IP的關鍵。然後使用該鍵從另一個字典中檢索匹配值。

編輯1: 回答了我自己。

回答

1

我最終使用列表理解和iteritems。 global_hostlist是必需的字典,hostlist是匹配的字典。

if ip in [host_ip[1]["ip"] for host_ip in self.global_hostlist.iteritems()]: 
    key = [host_ip[1]["HOSTID"] for host_ip in self.global_hostlist.iteritems() if host_ip[1]["ip"] == ip] 
    if key[0]: 
     self.hostlist[key[0]] = self.global_hostlist[key[0]] 
+0

如果您認爲您發現該決議將有助於其他用戶,隨意[自答案】(http://stackoverflow.com/help/self-answer)你的問題。否則,你可以[刪除你的帖子](http://stackoverflow.com/help/deleted-questions)。 –

+0

我是SO的新手用戶,剛剛實現。希望它能幫助別人。我自己回答了。謝謝 – WLatif