pythonjson格式_python中的自定义json格式

1586010002-jmsa.png

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))

Output:

[

"name": "L1",

"children": [

{

"name": "L1",

"children":[

{

"name":"L3",

"children":[{}]

},

{

"name":"L1",

"children":[{}]

}]

},

{

"name":"L2",

"children":[{}]

}

]

for the levels

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

['L1','L1','L3'],

['L1','L2'],

I also need to encode the count of each level

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)

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))

OUTPUT

[

{

"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": [

{}

]

}

]

}

]

}

]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值