原始json常见于后端:

{
  "1": {
    "identity": "node",
    "title": "",
    "children": {
      "1.1": {
        "identity": "leaf",
        "title": "主页",
        "href": "http://192.168.199.132:9000/./"
      },
      "1.2": {
        "identity": "leaf",
        "title": "[漏洞库]",
        "href": "http://192.168.199.132:9000/./"
      },
      "1.3": {
        "identity": "node",
        "title": "提权EXP",
        "children": {
          "1.3.1": {
            "identity": "leaf",
            "title": "零组提权EXP自动更新脚本",
            "href": "http://192.168.199.132:9000/提权EXP/1.html"
          },
          "1.3.2": {
            "identity": "node",
            "title": "Linux",
            "children": {
              "1.3.2.1": {
                "identity": "leaf",
                "title": "2004",
                "href": "http://192.168.199.132:9000/提权EXP/Linux/2004/1.html"
              },
              "1.3.2.2": {
                "identity": "leaf",
                "title": "2005",
                "href": "http://192.168.199.132:9000/提权EXP/Linux/2004/1.html"
              }
            }
          },
          "1.3.3": {
            "identity": "node",
            "title": "Windows",
            "children": {
              "1.3.3.1": {
                "identity": "leaf",
                "title": "Windows提权总结",
                "href": "http://192.168.199.132:9000/提权EXP/Win/2.html"
              },
              "1.3.3.2": {
                "identity": "leaf",
                "title": "Win辅助提权脚本",
                "href": "http://192.168.199.132:9000/提权EXP/Win/1.html"
              }
            }
          }
        }
      }
    }
  }
}

前端友好型json:

[{
  "identity": "node",
  "title": "",
  "children": [
    {
      "identity": "leaf",
      "title": "主页",
      "href": "http://192.168.199.132:9000/./",
      "key": "1.1"
    },
    {
      "identity": "leaf",
      "title": "[漏洞库]",
      "href": "http://192.168.199.132:9000/./",
      "key": "1.2"
    },
    {
      "identity": "node",
      "title": "提权EXP",
      "children": [
        {
          "identity": "leaf",
          "title": "零组提权EXP自动更新脚本",
          "href": "http://192.168.199.132:9000/提权EXP/1.html",
          "key": "1.3.1"
        },
        {
          "identity": "node",
          "title": "Linux",
          "children": [
            {
              "identity": "leaf",
              "title": "2004",
              "href": "http://192.168.199.132:9000/提权EXP/Linux/2004/1.html",
              "key": "1.3.2.1"
            },
            {
              "identity": "leaf",
              "title": "2005",
              "href": "http://192.168.199.132:9000/提权EXP/Linux/2004/1.html",
              "key": "1.3.2.2"
            }
          ],
          "key": "1.3.2"
        },
        {
          "identity": "node",
          "title": "Windows",
          "children": [
            {
              "identity": "leaf",
              "title": "Windows提权总结",
              "href": "http://192.168.199.132:9000/提权EXP/Win/2.html",
              "key": "1.3.3.1"
            },
            {
              "identity": "leaf",
              "title": "Win辅助提权脚本",
              "href": "http://192.168.199.132:9000/提权EXP/Win/1.html",
              "key": "1.3.3.2"
            }
          ],
          "key": "1.3.3"
        }
      ],
      "key": "1.3"
    }
  ],
  "key": "1"
}]

实现代码:

import json


def read_json():
    file_path = "/home/mrq/docker/0group/bbb.json"
    with open(file_path, "r") as Fr:
        json_data = json.load(Fr)
    return json_data


def dict2list(dict_data):
    dict_list = []
    for key in dict_data.keys():
        dict_list.append(dict_data.get(key))
    return dict_list


def dict_deal(dict_data, inherited="", current_parent=""):
    inherited = inherited + " " + current_parent
    inherited = inherited.strip()
    eval_string1 = eval_string2 = "dict_data"
    for parents in inherited.split():
        eval_string1 += f".get('{parents}').get('children')"
        eval_string2 += f"['{parents}']['children']"
    current_child_dict = eval(eval_string1)

    children_keys = current_child_dict.keys()

    for key in children_keys:
        leaf_dict = current_child_dict[key]
        leaf_dict["key"] = key
        current_child_dict[key] = leaf_dict
        if current_child_dict.get(key).get("identity") == "node":
            dict_deal(dict_data=dict_data, inherited=inherited, current_parent=key)

    dict_list = dict2list(current_child_dict)
    if eval_string2 == "dict_data":
        return dict2list(dict_data)
    eval_string2 = eval_string2[:-12]
    eval(eval_string2)["children"] = dict_list



if __name__ == '__main__':
    da = read_json()
    deal2 = dict_deal(dict_data=da)
    print(deal2)

注意点: 示例的原始json中包含 identity 和 children, 实际使用是需要替换为实际值

results matching ""

    No results matching ""

    results matching ""

      No results matching ""