gpt4 book ai didi

python - 构建 JSON 格式时出现问题。在字典和集合之间选择?

转载 作者:行者123 更新时间:2023-12-01 07:18:20 24 4
gpt4 key购买 nike

我正在构建 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所需的属性构建字典后,我的代码接近尾声(并且有疑问我是否应该使用集合模块):

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)

关于python - 构建 JSON 格式时出现问题。在字典和集合之间选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57839765/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com