过滤JSON响应给出一个JSON对象有许多键,如(属性):与Python列表理解
[{'name': 'Bob', 'infos': {'spam': 'eggs', 'foo': 'bar'}},
{'name': 'Tom'},
{'name': 'Lisa', 'infos': {'spam': 'qux', 'foo': 'baz'}}
...]
我希望用一个列表理解筛选出的条目,其中entry['infos']['spam'] == 'eggs'
我如果可能的话,我更喜欢列表理解,但到目前为止,我唯一的解决方案是使用多个.get()
s,其中最靠右的树最远(以避免KeyError
s在声明到达之前通过声明False
s)。
例如,
# Will obviously fail with KeyError
[each for each in my_json if each['infos']['spam'] == 'eggs']
# Works but requires a separate/additional `.get()`, and only works
# because it is returning False before it evaluates all conditions
[each for each in my_json if each.get('infos') and each.get('infos').get('spam') == 'eggs']
# Fails as all conditions will be evaluated before running
[each for each in my_json if all([each.get('infos'), each.get('infos').get('spam') == 'eggs'])]
# Not a list comprehension, but concise... and also doesn't work
filter(lambda x: x['infos']['spam'] == 'eggs', my_json)
有没有什么更好的办法来筛选我的JSON响应?我之所以要问的原因是一些API返回的json对象的关键利益深下来......不得不使用类似each.get('a') and each['a'].get('b') and each['a']['b'].get('c') == 'd'
的东西似乎耗尽只是为了验证each['a']['b']['c'] == 'd'
。
我想我总是可以使用try
except KeyError
。
mylist = []
for each in my_json:
try:
if each['infos']['spam'] == 'eggs':
mylist.append(each)
except KeyError:
pass
是否有明显的解决方案我失踪(最好在python3标准库),这将消除所有的工作方案中的冗余?
N.B.如果[PEP 463](http://legacy.python.org/dev/peps/pep-0463/)发生,那么您可以在每个['infos'] ['spam']的[my_json =='eggs'除KeyError:False]' – roippi 2014-10-20 00:28:59