如何使用列表路径获取json值

时间:2019-06-13 13:34:15

标签: python json list

  

大家好,我下面有一个json文本

data = {
"glossary": {
    "title": "example glossary",
    "GlossDiv": {
        "title": "S",
        "GlossList": {
            "GlossEntry": {
                "ID": "SGML",
                "SortAs": "SGML",
                "GlossTerm": "Standard Generalized Markup Language",
                "Acronym": "SGML",
                "Abbrev": "ISO 8879:1986",
                "GlossDef": {
                    "para": "A meta-markup language, used to create markup languages...",
                    "GlossSeeAlso": ["GML", "XML"]
                },
                "GlossSee": "markup"
            }
        }
    }
}

}

我创建了一个函数,该函数可以获取json所有字段的路径。 如下所示:

def get_paths(source):
paths = []
if isinstance(source, collections.MutableMapping):  # found a dict-like structure...
    for k, v in source.items():  # iterate over it; Python 2.x: source.iteritems()
        paths.append([k])  # add the current child path
        paths += [[k] + x for x in get_paths(v)]  # get sub-paths, extend with the current
# else, check if a list-like structure, remove if you don't want list paths included
elif isinstance(source, collections.Sequence) and not isinstance(source, str):
    #                          Python 2.x: use basestring instead of str ^
    for i, v in enumerate(source):
        paths.append([i])
        paths += [[i] + x for x in get_paths(v)]  # get sub-paths, extend with the current
return paths

现在您可以看到如下所示的所有路径:

[['glossary'],
['glossary', 'title'],
['glossary', 'GlossDiv'],
['glossary', 'GlossDiv', 'title'],
['glossary', 'GlossDiv', 'GlossList'],
['glossary', 'GlossDiv', 'GlossList', 'GlossEntry'],
['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'ID'],
['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'SortAs'],
['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossTerm'],
['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'Acronym'],
['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'Abbrev'],
['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossDef'],
['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossDef', 'para'],
['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossDef', 'GlossSeeAlso'],
['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossDef', 'GlossSeeAlso', 0],
['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossDef', 'GlossSeeAlso', 1],
['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossSee']]

如何遍历路径列表以获取json每个字段的值?

2 个答案:

答案 0 :(得分:3)

Python提供了json模块,该模块可以将JSON文件读入python dict数据结构中:

import json

with open("my_file.json", "r") as my_file:
    contents = json.loads(my_file.read())

之后,您可以将其视为dict

title = contents["glossary"]["title"]
glossDivTitle = contents["glossary"]["glossDiv"]["title"]
para = contents["glossary"]["glossDiv"]["glossList"]["glossEntry"]["glossDef"]["para"]
...

现在,假设您有一个在问题中创建的这样的列表:

lk = ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'Acronym']

以下是您可以用来检索相关值的函数:

def retrieve_value(key_list, dct):
    subdict = dct
    for k in key_list:
        subdict = subdict[k]
    return subdict

retrieve_value(lk, contents)

答案 1 :(得分:1)

我建议您使用json库。 https://www.w3schools.com/python/python_json.asp 您可以使用json.loads将json转换成dict,并且可以通过dict_name[key]

访问值

在编写函数之前尝试查找库。