【问题标题】:Parse/search trough cascaded arrays in JSON解析/搜索 JSON 中的级联数组
【发布时间】:2019-11-25 06:47:11
【问题描述】:

我需要阅读下面的 json 响应

我正在搜索一个特殊文件夹的 ID。 我知道深度级别,我希望在哪里找到文件夹,因为我知道整个路径。 我需要取回文件夹的 ID。

我所知道的是我正在寻找的文件夹的整个路径,例如 /Core/UI/Folder1/Subfolder2 所以我需要 subfolder2 的 ID。

我尝试了几个循环,但由于深入搜索而失败。 当然,我可以编写一些手动级别的深度代码,但这听起来不是我认为的正确方式。

示例 JSON

{
    "folders": [
        {
            "rank": 1,
            "name": "Core",
            "id": 390,
            "testCount": 0,
            "totalTestCount": 0,
            "testRepositoryPath": "",
            "folders": [
                {
                    "rank": 1,
                    "name": "UI",
                    "id": 391,
                    "testCount": 0,
                    "totalTestCount": 0,
                    "testRepositoryPath": "/Core",
                    "folders": [
                        {
                            "rank": 1,
                            "name": "Folder1",
                            "id": 392,
                            "testCount": 0,
                            "totalTestCount": 0,
                            "testRepositoryPath": "/Core/UI",
                            "folders": [
                                {
                                    "rank": 1,
                                    "name": "Subfolder2",
                                    "id": 393,
                                    "testCount": 0,
                                    "totalTestCount": 0,
                                    "testRepositoryPath": "/Core/UI/Folder1",
                                    "folders": []
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ],
    "allTestsCount": 791,
    "allOrphanTestsCount": 791
}

【问题讨论】:

  • 是基于“name”还是“testRepositoryPath”?
  • name 和 testRepositoryPath 的组合将匹配整个路径
  • 您想要的路径与您的 JSON 不匹配。请解决您的问题,并确保您从您拥有的东西中准确地确定您想要的东西。告诉我们输入和期望的输出。

标签: json python-3.x for-loop


【解决方案1】:

好的,如果我理解正确,这应该可以,请告诉我:

data = {
    "folders": [
        {
            "rank": 1,
            "name": "Core",
            "id": 390,
            "testCount": 0,
            "totalTestCount": 0,
            "testRepositoryPath": "",
            "folders": [
                {
                    "rank": 1,
                    "name": "UI",
                    "id": 391,
                    "testCount": 0,
                    "totalTestCount": 0,
                    "testRepositoryPath": "/Core",
                    "folders": [
                        {
                            "rank": 1,
                            "name": "Folder1",
                            "id": 392,
                            "testCount": 0,
                            "totalTestCount": 0,
                            "testRepositoryPath": "/Core/UI",
                            "folders": [
                                {
                                    "rank": 1,
                                    "name": "Subfolder2",
                                    "id": 393,
                                    "testCount": 0,
                                    "totalTestCount": 0,
                                    "testRepositoryPath": "/Core/UI/Folder1",
                                    "folders": []
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ],
    "allTestsCount": 791,
    "allOrphanTestsCount": 791
}


def extract_inner_id(d, path):
    try:
        current_path = d['testRepositoryPath'] + '/' + d['name']
        if current_path == path:
            return d['id']
    except KeyError:
        pass
    for sub_d in d['folders']:
        inner_id = extract_inner_id(sub_d, path)
        if inner_id is not None:
            return inner_id
    return None


print(extract_inner_id(data, "/Core/UI/Folder1/Subfolder2"))

请注意,对于每个子文件夹,我们都会在其内部调用 extract_inner_id,这就是我们搜索深度的方式。

【讨论】:

  • @JörgLang 也许我没有正确理解。这个条件对吗? if sub_d['testRepositoryPath'] + '/' + sub_d['name'] == path: ?
  • 我已经更新了原始帖子中的 JSON.... 我会说是的 sub_d['testRepositoryPath'] + '/' + sub_d['name'] == path 是正确的,如果 Im没错
  • @JörgLang 好的,找到了!它与顶级数据没有“名称”和“testRepositoryPath”的事实有关。请立即尝试:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-19
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多