将Python字典转换为具有特定格式的JSON
我正在使用python解析excel电子表格,使用数据列创建分层树结构。我已成功地将其解析为python字典,可以将其json转储为以下格式:将Python字典转换为具有特定格式的JSON,python,json,dictionary,Python,Json,Dictionary,我正在使用python解析excel电子表格,使用数据列创建分层树结构。我已成功地将其解析为python字典,可以将其json转储为以下格式: { "1": { "1a": [ "Apple", "Orange" ], "1b": [ "Horse" ] }, "2": { "2b": [ "Spinach", "Nut" ] } } 但我需要将其转换为最终的json输出
{
"1": {
"1a": [
"Apple",
"Orange"
],
"1b": [
"Horse"
]
},
"2": {
"2b": [
"Spinach",
"Nut"
]
}
}
但我需要将其转换为最终的json输出格式:
[
{
"name":"1",
"children": [
{
"name":"1a",
"children":
[
{"name":"Apple"},
{"name":"Orange"}
]
},
{
"name":"1b",
"children": [
{"name":"Horse"}
]
}
]
},
{
"name":"2",
"children": [
{
"name":"2b",
"children": [
{"name":"Spinach"},
{"name":"Nut"}
]
}
]
}
]
有没有办法操纵python字典或更改json来实现这一点?我是python新手,我觉得这里肯定缺少一些东西。这是关于重新格式化数据,而不是json本身 这是我想到的,有点混乱
import json
x = {"1": {"1a": ["Apple", "Orange"], "1b": ["Horse"]}, "2": {"2b": ["Spinach", "Nut"]}}
def conv(k, v):
return {"name": k, "children": list({"name": x, "children": list({"name": z} for z in v[x])} for x in v)}
print json.dumps(list(conv(k, x[k]) for k in x), indent=4)
我希望这会有所帮助这是关于重新格式化数据,而不是json本身 这是我想到的,有点混乱
import json
x = {"1": {"1a": ["Apple", "Orange"], "1b": ["Horse"]}, "2": {"2b": ["Spinach", "Nut"]}}
def conv(k, v):
return {"name": k, "children": list({"name": x, "children": list({"name": z} for z in v[x])} for x in v)}
print json.dumps(list(conv(k, x[k]) for k in x), indent=4)
我希望这会有所帮助这是关于重新格式化数据,而不是json本身 这是我想到的,有点混乱
import json
x = {"1": {"1a": ["Apple", "Orange"], "1b": ["Horse"]}, "2": {"2b": ["Spinach", "Nut"]}}
def conv(k, v):
return {"name": k, "children": list({"name": x, "children": list({"name": z} for z in v[x])} for x in v)}
print json.dumps(list(conv(k, x[k]) for k in x), indent=4)
我希望这会有所帮助这是关于重新格式化数据,而不是json本身 这是我想到的,有点混乱
import json
x = {"1": {"1a": ["Apple", "Orange"], "1b": ["Horse"]}, "2": {"2b": ["Spinach", "Nut"]}}
def conv(k, v):
return {"name": k, "children": list({"name": x, "children": list({"name": z} for z in v[x])} for x in v)}
print json.dumps(list(conv(k, x[k]) for k in x), indent=4)
我希望这会有所帮助您的输出JSON不太正确,但这显示了如何生成类似的内容(缺少几个列表/数组括号):
您的输出JSON不太正确,但这显示了如何生成类似的内容(它缺少几个列表/数组括号):
您的输出JSON不太正确,但这显示了如何生成类似的内容(它缺少几个列表/数组括号):
您的输出JSON不太正确,但这显示了如何生成类似的内容(它缺少几个列表/数组括号):
问题中显示的JSON输出缺少一些括号字符。无论如何,我认为您应该做的是将现有的分层树结构重新格式化为需要转储的结构:
import json
tree = {
"1": {
"1a": [
"Apple",
"Orange"
],
"1b": [
"Horse"
]
},
"2": {
"2b": [
"Spinach",
"Nut"
]
}
}
objects = [
{'name': branch,
'children': [
{'name': subbranch,
'children': [{'name': subchild} for subchild in subchildren]
} for subbranch, subchildren in children.items()
]
} for branch, children in tree.items()
]
print(json.dumps(objects, indent=2))
问题中显示的JSON输出缺少一些括号字符。无论如何,我认为您应该做的是将现有的分层树结构重新格式化为需要转储的结构:
import json
tree = {
"1": {
"1a": [
"Apple",
"Orange"
],
"1b": [
"Horse"
]
},
"2": {
"2b": [
"Spinach",
"Nut"
]
}
}
objects = [
{'name': branch,
'children': [
{'name': subbranch,
'children': [{'name': subchild} for subchild in subchildren]
} for subbranch, subchildren in children.items()
]
} for branch, children in tree.items()
]
print(json.dumps(objects, indent=2))
问题中显示的JSON输出缺少一些括号字符。无论如何,我认为您应该做的是将现有的分层树结构重新格式化为需要转储的结构:
import json
tree = {
"1": {
"1a": [
"Apple",
"Orange"
],
"1b": [
"Horse"
]
},
"2": {
"2b": [
"Spinach",
"Nut"
]
}
}
objects = [
{'name': branch,
'children': [
{'name': subbranch,
'children': [{'name': subchild} for subchild in subchildren]
} for subbranch, subchildren in children.items()
]
} for branch, children in tree.items()
]
print(json.dumps(objects, indent=2))
问题中显示的JSON输出缺少一些括号字符。无论如何,我认为您应该做的是将现有的分层树结构重新格式化为需要转储的结构:
import json
tree = {
"1": {
"1a": [
"Apple",
"Orange"
],
"1b": [
"Horse"
]
},
"2": {
"2b": [
"Spinach",
"Nut"
]
}
}
objects = [
{'name': branch,
'children': [
{'name': subbranch,
'children': [{'name': subchild} for subchild in subchildren]
} for subbranch, subchildren in children.items()
]
} for branch, children in tree.items()
]
print(json.dumps(objects, indent=2))
您的json输出格式不是有效的json。可能缺少“]”?您的json输出格式不是有效的json。可能缺少“]”?您的json输出格式不是有效的json。可能缺少“]”?您的json输出格式不是有效的json。可能缺少“]'?