python中的自定义json格式 [英] custom json formatting in python

查看:310
本文介绍了python中的自定义json格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来生成列表列表的json表示形式.

I have the following code to generate json representation of 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)

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



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

输出:

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

      ]

关卡

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

我还需要对每个级别的计数进行编码

I also need to encode the count of each level

例如,存在从L1开始的路径,该路径具有两个第一级子级L1(2)L2(1),后跟L2(1)L3(1)用于下一级.

for eg there is the path from L1 which has two first level childrens L1(2) and L2(1) followed by L2(1) and L3(1) for next level .

L1(3)-->L1(2)-->L2(1)
             -->L3(1)
     -->L2(1)

如何在我的json输出中编码此计数.

How can I encode this count in my json output.

我希望我的最终输出看起来像这样

I want my final output to look like this

"name": "L1(3)",
      "children": [
        {
          "name": "L1(2)",
           "children":[

推荐答案

root={}
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)

def convert(d):
    templist=[]
    noofchildren=0
    if(len(d.items())==0):
        return ([{}],1)
    for k,v in d.items():
        temp,children=convert(v)
        noofchildren+=children
        if(temp):
            templist.append({"name":k+"("+str(children)+")",'children':temp})
        else:
            templist.append({'name': k+"("+str(children)+")", 'children':[{}]})

    return (templist,noofchildren)    

# Print results
import json
print(json.dumps(convert(root)[0],  indent=2))

输出

[
  {
    "name": "L1(3)",
    "children": [
      {
        "name": "L1(2)",
        "children": [
          {
            "name": "L2(1)",
            "children": [
              {}
            ]
          },
          {
            "name": "L3(1)",
            "children": [
              {}
            ]
          }
        ]
      },
      {
        "name": "L2(1)",
        "children": [
          {}
        ]
      }
    ]
  },
  {
    "name": "L2(2)",
    "children": [
      {
        "name": "L2(2)",
        "children": [
          {
            "name": "L3(1)",
            "children": [
              {}
            ]
          },
          {
            "name": "L1(1)",
            "children": [
              {}
            ]
          }
        ]
      }
    ]
  },
  {
    "name": "L3(1)",
    "children": [
      {
        "name": "L2(1)",
        "children": [
          {}
        ]
      }
    ]
  },
  {
    "name": "L4(2)",
    "children": [
      {
        "name": "L2(2)",
        "children": [
          {
            "name": "L1(1)",
            "children": [
              {}
            ]
          },
          {
            "name": "L4(1)",
            "children": [
              {}
            ]
          }
        ]
      }
    ]
  }
]

这篇关于python中的自定义json格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆