繁体   English   中英

如何格式化嵌套的 python 字典并将其写入 json 文件?

[英]How do I format a nested python dictionary and write it to a json file?

我希望在我的 Django 视图中运行计算,然后将其写入 json 文件。 但是,我正在努力使格式正确。

我需要 output 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 文件中得到的 output 是:

[[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"]}]]]]

谢谢!

您不需要将 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)

您有labelsincomeexpenses ,每个list都包含一个dict 你只需要dict

你有datayeardates当它们需要是字典时作为列表。

您只需要以正确的形式获取所有内容:

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)

只需将列表更改为字典:

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
}

暂无
暂无

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

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