python - 用python格式化JSON
问题描述
我想以这种形式发送一个 api 请求:
"line_items": [
{
"account_id": "1717893000000067010",
"debit_or_credit": "debit",
"amount": 400,
"tags": [
{
"tag_option_id": "1717893000000115007",
"tag_id": "1717893000000000333"
},
{
"tag_option_id": "1717893000000123007",
"tag_id": "1717893000000000335"
},
{
"tag_option_id": "1717893000000126003",
"tag_id": "1717893000000000337"
}
]
上面的 JSON 可能有数百个 (line_items) 并且其中的每个 (tags) 可能有不同数量的字典。
我在 python 中所做的是:
accounts = []
tags = []
for line in payroll.line_ids:
######## code missing some correction for tags
if len(line.x_zoho_jtag) == 0:
the_tags = {"tag_id": " ", "tag_option_id": " "}
tags.append(the_tags)
for tag in line.x_zoho_jtag:
for option in line.x_zoho_jtag_option:
if option.tag_ids == tag.tag_id:
the_tags = {"tag_id": tag.tag_id, "tag_option_id": option.option_tag_id}
tags.append(the_tags)
########
if line.debit != 0.0:
credit = {"amount": line.debit,"account_id": line.x_zoho_account_no,"debit_or_credit": "debit", "tags": tags}
accounts.append(credit)
print(credit)
else:
debit = {"amount": line.credit, "account_id": line.x_zoho_account_no,"debit_or_credit": "credit", "tags": tags}
accounts.append(debit)
print(debit)
print(accounts)
正如您在上面的 python 代码中看到的,我有 2 个列表(帐户和标签)。我将 (account_id, debit_or_credit, amount) 存储在 (accounts) 列表中,它工作正常。
if line.debit != 0.0:
credit = {"amount": line.debit,"account_id": line.x_zoho_account_no,"debit_or_credit": "debit", "tags": tags}
accounts.append(credit)
print(credit)
else:
debit = {"amount": line.credit, "account_id": line.x_zoho_account_no,"debit_or_credit": "credit", "tags": tags}
accounts.append(debit)
print(debit)
除此之外,我还添加了 (tags) 键和 (tags) 列表,如您在上面的行中所见。
我面临的问题是在 (tags) 键中,我需要在列表中传递多个字典块。那么该怎么做呢?
预期输出:
"line_items": [
{
"account_id": "1717893000000067010",
"debit_or_credit": "debit",
"amount": 400,
"tags": [
{
"tag_option_id": " ",
"tag_id": " "
},
{
"tag_option_id": "1717893000000126003",
"tag_id": "1717893000000000337"
},
{
"tag_option_id": "1717893000000123007",
"tag_id": "1717893000000000335"
}
"line_items": [
{
"account_id": "1717893000000067036",
"debit_or_credit": "credit",
"amount": 400,
"tags": [
{
"tag_option_id": "1717893000000126003",
"tag_id": "1717893000000000337"
}
错误的输出:
{
"account_id": "1717893000000067010",
"debit_or_credit": "debit",
"amount": 400,
"tags": [
{
"tag_option_id": " ",
"tag_id": " "
},
{
"tag_option_id": "1717893000000126003",
"tag_id": "1717893000000000337"
},
{
"tag_option_id": "1717893000000123007",
"tag_id": "1717893000000000335"
}
{
"account_id": "1717893000000067036",
"debit_or_credit": "credit",
"amount": 400,
"tags": [
{
"tag_option_id": " ",
"tag_id": " "
},
{
"tag_option_id": "1717893000000126003",
"tag_id": "1717893000000000337"
},
{
"tag_option_id": "1717893000000123007",
"tag_id": "1717893000000000335"
}
解决方案
这里的问题是,当您循环浏览标签时,您并没有区分应该放在 上的credit
那些和应该放在debit
.
您需要做的是首先获取该行,然后获取该行的关联标签。
我认为下面应该可以工作,但有一些重复,因此可以进一步改进。
accounts = []
for line in payroll.line_ids:
if line.debit != 0.0:
credit = {
"amount": line.debit,
"account_id": line.x_zoho_account_no,
"debit_or_credit": "debit",
"tags": []
}
if len(line.x_zoho_jtag) == 0:
credit["tags"].append({"tag_id": " ", "tag_option_id": " "})
else:
for tag in line.x_zoho_jtag:
for option in line.x_zoho_jtag_option:
if option.tag_ids == tag.tag_id:
credit["tags"].append({"tag_id": tag.tag_id,
"tag_option_id": option.option_tag_id})
accounts.append(credit)
print(credit)
else:
debit = {
"amount": line.credit,
"account_id": line.x_zoho_account_no,
"debit_or_credit": "credit",
"tags": []
}
if len(line.x_zoho_jtag) == 0:
debit["tags"].append({"tag_id": " ", "tag_option_id": " "})
else:
for tag in line.x_zoho_jtag:
for option in line.x_zoho_jtag_option:
if option.tag_ids == tag.tag_id:
debit["tags"].append({"tag_id": tag.tag_id,
"tag_option_id": option.option_tag_id})
accounts.append(debit)
print(debit)
进一步重构
将重复的代码块移动到函数中
accounts = []
for line in payroll.line_ids:
if line.debit != 0.0:
credit = create_account("credit", line)
accounts.append(credit)
print(credit)
else:
debit = create_account("debit", line)
accounts.append(debit)
print(debit)
def create_account(account_type, line):
if account_type == "credit":
amount = line.debit
d_or_c = "debit"
else:
amount = line.credit
d_or_c = "credit"
account = {
"amount": amount,
"account_id": line.x_zoho_account_no,
"debit_or_credit": d_or_c,
"tags": []
}
if len(line.x_zoho_jtag) == 0:
account["tags"].append({"tag_id": " ", "tag_option_id": " "})
else:
for tag in line.x_zoho_jtag:
for option in line.x_zoho_jtag_option:
if option.tag_ids == tag.tag_id:
account["tags"].append({"tag_id": tag.tag_id,
"tag_option_id": option.option_tag_id})
return account
推荐阅读
- css - 为什么计算出的身高为 100% 的身体高度与其实际高度不匹配?
- python - rdd.first() 不会给出错误,但 rdd.collect() 会
- javascript - 在 HTML5 画布中处理大图像
- android - Gluon Mobile for Android = StackOverflowError 设备依赖
- r - 将累积值转换为单个值
- git - Gitlab Pre-Receive Hook 拒绝 SSH 推送
- c++ - 重新排序 QTreeWidgetItem 后 QTreeWidgetItem 中的 QWidget 消失
- angular - ReferenceError:找不到变量:区域
- regex - 正则表达式查找和替换小写大写
- c++ - 如何从 int 一次写入一个字节的文件?C++