我正在构建json格式,预期如下所示:
{
accounts:[ {
"acctnum": "acct1",
"key2":"value2"
"key3": []
"summary" : {
//nested dict
}
}
] //if we have 1 account for given customer
}
如果我们有多个客户帐户:
{
accounts:[ {
"acctnum": "acct1", // for acct1
"key2":"value2"
"key3": []
"summary" : {
//nested dict
}
},
{
"acctnum": "acct2", //for acct2
"key2":"value3"
"key3": []
"summary" : {
//nested dict
}
}
]
}
用acct1的必需属性构建字典后,我的结尾代码是(并且有疑问是否应该使用collections模块):
acctlist = []
acctlist = results //results is dict for acct1 (with nested dict)
print(acctlist)
accounts = {}
accounts["accounts"] = acctlist
j = json.dumps(accounts, indent=4)
print(j)
但是实际的json格式显示为:
{
accounts: {
"acctnum": "acct1",
"key2":"value2"
"key3": []
"summary1" : {
//nested dict
}
}
}
最佳答案
您的印刷声明可能会回答您的问题。是清单吗?
acctlist = []
acctlist = results //results is dict for acct1 (with nested dict)
print(acctlist)
我怀疑您打算这样做
acctlist.append(results)