Python:如果jc中的abc ['类别']

时间:2014-12-11 14:50:00

标签: python json list python-2.7

给定一个如下所示的JSON文件:

"foobar": [
    {
        "a": "true",
        "b": 1,
        "c": 1234,
        "d": 9,
        "e": "red"
    },
    {
        "a": "false",
        "b": 2,
        "c": 2345,
        "d": 7,
        "e": "green"
    },
    {
        "a": "whocares",
        "b": 3,
        "c": 3456,
        "d": 5,
        "e": "blue"
    }
]

是否可以检查任何中是否存在“蓝色”,没有循环?

import simplejson
j = json.loads(superfile().text)
if "blue" in j['foobar'][ANYONE]['e']

1 个答案:

答案 0 :(得分:6)

@jonrsharpe所述,您最好的选择可能是使用any,例如

if any('blue' in i['e'] for i in j['foobar'])

或者

if any('blue' == i['e'] for i in j['foobar'])