提问人:Rno 提问时间:1/5/2023 更新时间:1/5/2023 访问量:41
如何格式化嵌套的python字典并将其写入json文件?
How do I format a nested python dictionary and write it to a json file?
问:
我希望在我的 Django 视图中运行计算,然后将其写入 json 文件。但是,我正在努力使格式正确。
我需要输出json文件看起来像这样:
{
"dates":
{
"year":
{
"total": 1586266,
"upDown": 9.8,
"data": {
"labels": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
"income": [2000, 4000, 6000, 8000, 10000, 12000, 14000, 16000, 18000, 20000, 22000, 24000],
"expenses": [6000, 12000, 18000, 24000, 30000, 36000, 42000, 48000, 54000, 60000, 66000, 72000]
}
}
}
}
以下是我的观点:
def generateGraph():
income = [{'income':['2000','2000','2000','2000','2000','2000','2000','2000','2000','2000','2000','2000']}]
expenses = [{'expenses':['1000','1000','1000','1000','1000','1000','1000','1000','1000','1000','1000','1000']}]
labels = [{'labels':['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']}]
total = 1586266
upDown = 9.8
data = [labels, income, expenses]
year = [total,upDown,data]
dates = [year]
with open(
"static/graph.json") as f:json.dump(dates, f)
return HttpResponse(open("static/graph.json", 'r'), content_type = 'application/json; charset=utf8')
但是,我目前在json文件中得到的输出是:
[[1586266, 9.8, [[{"labels": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]}], [{"income": ["2000", "2000", "2000", "2000", "2000", "2000", "2000", "2000", "2000", "2000", "2000", "2000"]}], [{"expenses": ["1000", "1000", "1000", "1000", "1000", "1000", "1000", "1000", "1000", "1000", "1000", "1000"]}]]]]
谢谢!
答:
0赞
quamrana
1/5/2023
#1
你有 、 和 ,每个都带有一个。您只需要 .labels
income
expenses
list
dict
dict
你有 和 作为列表,当它们需要成为字典时。data
year
dates
你只需要以正确的形式获得所有内容:
def generateGraph():
income = {'income':['2000','2000','2000','2000','2000','2000','2000','2000','2000','2000','2000','2000']}
expenses = {'expenses':['1000','1000','1000','1000','1000','1000','1000','1000','1000','1000','1000','1000']}
labels = {'labels':['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']}
total = 1586266
upDown = 9.8
data = labels | income | expenses
year = {'total':total, 'upDown':upDown, 'data':data}
dates = {'year': year}
with open("static/graph.json") as f:
json.dump({'dates: dates}, f)
0赞
Amin Rashidbeigi
1/5/2023
#2
只需将列表更改为字典:
income = [{'income':['2000','2000','2000','2000','2000','2000','2000','2000','2000','2000','2000','2000']}]
expenses = [{'expenses':['1000','1000','1000','1000','1000','1000','1000','1000','1000','1000','1000','1000']}]
labels = [{'labels':['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']}]
total = 1586266
upDown = 9.8
data = {
"labels": labels,
"income": income,
"expenses":expenses
}
year = {
"total": total,
"upDown": upDown,
"data": data
}
dates = {
"year":year
}
2赞
kjaw
1/5/2023
#3
您无需将 json 保存到文件中。
你只需要使用 JsonResponse
def generateGraph():
data = {
"dates": {
"year": {
"total": 1586266,
"upDown": 9.8,
"data": {
"labels": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
"income": [2000, 4000, 6000, 8000, 10000, 12000, 14000, 16000, 18000, 20000, 22000, 24000],
"expenses": [6000, 12000, 18000, 24000, 30000, 36000, 42000, 48000, 54000, 60000, 66000, 72000]
}
}
}
}
return JsonResponse(data)
上一个:从 json 获取嵌套列表
下一个:从具有多层嵌套数组的输入创建列表
评论