简体   繁体   English

使用格式将python字符串转换为json

[英]convert python string to json with formatting

I have following code that generates parent child relationship in list of lists 我有以下代码在列表列表中生成父子关系

Levels=[['L1','L1','L2'],
        ['L1','L1','L3'],
        ['L1','L2'],
        ['L2','L2','L3'],
        ['L2','L2','L1'],
        ['L3','L2'],
        ['L4','L2','L1'],
        ['L4','L2','L4']]

def append_path(root, paths):
    if paths:
        child = root.setdefault(paths[0], {})
        append_path(child, paths[1:])

for p in Levels:
    append_path(root, p)

# Print results
import json
print(json.dumps(root,  indent=4))

The output generated is in the following format 生成的输出采用以下格式

{
    "L1": {
        "L1": {
            "L2": {},
            "L3": {}
        },
        "L2": {}
    },
    "L2": {
        "L2": {
            "L3": {},
            "L1": {}
        }
    },
    "L3": {
        "L2": {}
    },
    "L4": {
        "L2": {
            "L1": {},
            "L4": {}
        }
    }
}

I want to convert this into the following format which is required for my jquery visualization. 我想将其转换为我的jquery可视化所需的以下格式。

      "name": "L1",
      "children": [
        {
          "name": "L1",
           "children":[
              {
                "name":"L3",
                "children":[{}]
              },
              {
                "name":"L1",
                "children":[{}]
              }]
        },
        {
            "name":"L2",
            "children":[{}]
        }

      ]

and so on 等等

You can do this recursively (after you've built root in the format you posted in your question): 您可以递归地执行此操作(在以问题中发布的格式构建root之后):

def convert(d):
    return [{'name': k, 'children': convert(v) if v else [{}]} for k, v in d.items()]

print(json.dumps(convert(root),  indent=2))

Output 产量

[
  {
    "children": [
      {
        "children": [
          {
            "children": [
              {}
            ],
            "name": "L3"
          },
          {
            "children": [
              {}
            ],
            "name": "L1"
          }
        ],
        "name": "L2"
      }
    ],
    "name": "L2"
  },
  {
    "children": [
      {
        "children": [
          {}
        ],
        "name": "L2"
      }
    ],
    "name": "L3"
  },
  {
    "children": [
      {
        "children": [
          {
            "children": [
              {}
            ],
            "name": "L4"
          },
          {
            "children": [
              {}
            ],
            "name": "L1"
          }
        ],
        "name": "L2"
      }
    ],
    "name": "L4"
  },
  {
    "children": [
      {
        "children": [
          {}
        ],
        "name": "L2"
      },
      {
        "children": [
          {
            "children": [
              {}
            ],
            "name": "L2"
          },
          {
            "children": [
              {}
            ],
            "name": "L3"
          }
        ],
        "name": "L1"
      }
    ],
    "name": "L1"
  }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM