2016-11-16 55 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