使用python格式化JSON

问题描述 投票:0回答:2

我想以这种形式发送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),其中的每个(标签)可能有不同数量的字典。

我在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,金额),它工作正常。

  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)

除此之外,我还添加了(标签)键和(标签)列表,如上图所示。

我面临的问题是在(标签)键中我需要在列表中传递多个字典块。那怎么办?

预期产量:

  "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"
  }
python python-3.x odoo erp
2个回答
1
投票

这里的问题是,当你循环遍历标记时,你不区分应该在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)

Refactored Further

将重复代码块移动到函数中

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

0
投票

我想我很快就会看到你...电影里面的标签= []在顶级循环中。

© www.soinside.com 2019 - 2024. All rights reserved.