在Python中从JSON数据编译多个关键内容

在Python中从JSON数据编译多个关键内容,python,json,Python,Json,我使用Python,数据以JSON格式存储。 我想编译与某个键对应的所有数据。例如 {"menu": { "header": "SVG Viewer", "items": [ {"id": "Open"}, {"id": "OpenNew", "label": "Open New"}, null, {"id": "ZoomIn", "label": "Zoom In"}, {"id": "ZoomOu

我使用Python,数据以JSON格式存储。 我想编译与某个键对应的所有数据。例如

{"menu": {
    "header": "SVG Viewer",
    "items": [
        {"id": "Open"},
        {"id": "OpenNew", "label": "Open New"},
        null,
        {"id": "ZoomIn", "label": "Zoom In"},
        {"id": "ZoomOut", "label": "Zoom Out"},
        {"id": "OriginalView", "label": "Original View"},
        null,
        {"id": "Quality"},
        {"id": "Pause"},
        {"id": "Mute"},
        null,
        {"id": "Find", "label": "Find..."},
        {"id": "FindAgain", "label": "Find Again"},
        {"id": "Copy"},
        {"id": "CopyAgain", "label": "Copy Again"},
        {"id": "CopySVG", "label": "Copy SVG"},
        {"id": "ViewSVG", "label": "View SVG"},
        {"id": "ViewSource", "label": "View Source"},
        {"id": "SaveAs", "label": "Save As"},
        null,
        {"id": "Help"},
        {"id": "About", "label": "About Adobe CVG Viewer..."}
    ]
}}
我想把所有“标签”键的内容编译成一个列表

有没有一种方法可以做到这一点而不必“走下树”,也就是不必迭代包含的键(“菜单”>>“项”>>“标签”)

最好,我正在寻找一个基于JSON的解决方案,而不是使用正则表达式定位内容的技巧。

您可以使用:


请注意,这基本上为您提供了“下树”功能,但其更高级别的界面使其更易于使用。

不,您必须“下树”<代码>[item['label']用于树['menu']['items']].中的项目['label']看起来没有那么痛苦,但是基于JSON的解决方案?JSON是一种数据格式,而不是代码。
import json
from jsonpath_rw import parse

d = json.loads("""{"menu": {
        "header": "SVG Viewer",
        "items": [
            {"id": "Open"},
            {"id": "OpenNew", "label": "Open New"},
            null,
            {"id": "ZoomIn", "label": "Zoom In"},
            {"id": "ZoomOut", "label": "Zoom Out"},
            {"id": "OriginalView", "label": "Original View"},
            null,
            {"id": "Quality"},
            {"id": "Pause"},
            {"id": "Mute"},
            null,
            {"id": "Find", "label": "Find..."},
            {"id": "FindAgain", "label": "Find Again"},
            {"id": "Copy"},
            {"id": "CopyAgain", "label": "Copy Again"},
            {"id": "CopySVG", "label": "Copy SVG"},
            {"id": "ViewSVG", "label": "View SVG"},
            {"id": "ViewSource", "label": "View Source"},
            {"id": "SaveAs", "label": "Save As"},
            null,
            {"id": "Help"},
            {"id": "About", "label": "About Adobe CVG Viewer..."}
        ]
    }}""")

    get_labels = parse("menu.items[*].label")

    for match in get_labels.find(d):
        print match.value   # value of label