访问json键,使用python键而不是索引

时间:2015-07-10 16:25:13

标签: python json

我有如下所示的JSON文件。我是编程的新手,经过大量的试验和错误,我能够通过文件中的索引访问单个键,值,如下面的代码所示。我希望能够通过多个对象按键搜索。例如,我想用关键字“vedette”搜索并返回“academieFrançaise”,“Quai conti”,“Institut”,“Les quarante”。关于如何做到这一点的任何建议都会很棒。

with open('DESformatted.json') as data_file:    
    d = json.load(data_file)
pprint (d['DES'][0]['vedette'])

[u'Acad\xe9mie fran\xe7aise']

{
    "DES": [
        {
            "vedette": ["Académie française"],
            "synonymes": [
                "Institut",
                "Quai Conti",
                "les Quarante"
            ],
            "liste10": [
                "Institut",
                "66",
                "Quai Conti",
                "66",
                "les Quarante",
                "66"
            ],
            "cliques": [
                [
                    "Académie française",
                    "Institut"
                ],
                [
                    "Académie française",
                    "Quai Conti"
                ],
                [
                    "Académie française",
                    "les Quarante"
                ]
            ]
        },
        {
            "vedette": ["Quai Conti"],
            "synonymes": [
                "Académie française"
            ],
            "liste10": [
                "Académie française",
                "200"
            ],
            "cliques": [
                [
                    "Académie française",
                    "Quai Conti"
                ]
            ]
        },
        {
            "vedette": ["les Quarante"],
            "synonymes": [
                "Académie française"
            ],
            "liste10": [
                "Académie française",
                "200"
            ],
            "cliques": [
                [
                    "Académie française",
                    "les Quarante"
                ]
            ]
        },
        {
            "vedette": ["Institut"],
            "synonymes": [
                "Académie française",
                "académie"
            ],
            "liste10": [
                "Académie française",
                "100",
                "académie",
                "100"
            ],
            "cliques": [
                [
                    "Académie française",
                    "Institut"
                ],
                [
                    "Institut",
                    "académie"
                ]
            ]
        }
    ]
}

3 个答案:

答案 0 :(得分:0)

"DES"相关联的值是一个对象列表,因此您可以像这样迭代它们:

for obj in d["DES"]:
    print(obj["vedette"])

输出:

['Acad\xe9mie fran\xe7aise']
['Quai Conti']
['les Quarante']
['Institut']

答案 1 :(得分:0)

您可以执行以下操作:

>>> vedettes = []

>>> for x in d['DES']:
    vedettes += x['vedette']

>>> vedettes
['Acad\xc3\xa9mie fran\xc3\xa7aise', 'Quai Conti', 'les Quarante', 'Institut']

我在这里迭代字典DES的{​​{1}}键中的所有值。然后我将从每次迭代中获得的d值存储到vedette列表中。这是通过添加获得的vedettes列表和vedette列表来完成的,这样每个步骤中获得的vedettes列表的所有元素都会添加到最终的vedette列表中。< / p>

vedettes列表包含您想要的输出。

答案 2 :(得分:0)

作为其他人提供的正确for循环的替代方法,您可以使用list comprehension

>>> import json
>>> d = json.load(open('DESformatted.json'))
>>> [x['vedette'] for x in d['DES']]
[[u'Acad\xe9mie fran\xe7aise'], [u'Quai Conti'], [u'les Quarante'], [u'Institut']]
>>>