【问题标题】:How to handle POST request to API with multiple JSON objects如何使用多个 JSON 对象处理对 API 的 POST 请求
【发布时间】:2019-08-28 11:20:47
【问题描述】:

我有一个 API 需要通过 POST 请求接收多张发票。传入的 JSON 如下所示。 我的模型看起来就像 JSON 对象。

如果我发送一张发票,一切正常,处理工作正常。如果我发送两张发票(就像示例一样),我只会获得第二张发票的数据(我假设这是因为那是要反序列化的最后一张发票)。有没有办法让我“循环”发票并处理每张发票?

对于我是 C# 和 API 开发的初学者,我提前道歉

{
"Invoices": {
    "Invoice": {
        "SellerParty": {
            "SellerPartyAddress": {
                "Name": "The Company111",
                "AddressLine1": "Street",
                "AddressLine2": "Box 111",
                "ZipCode": "123456",
                "City": "STHLM",
                "Country": "Sweden",
                "CountryCode": "SE"
            },
            "SellerPartyInfo": {
                "WebAddress": "www.thecompany.com",
                "PhoneNumber": "123456789",
                "EmailAddress": "info@thecompany.com"
            },
            "SellerPartyPaymentMeans": {
                "IBAN": "123455670",
                "BICSWIFT": "000000000000",
                "BankAccount": "1111111111111"
            }
        },
        "BuyerParty": {
            "BuyerPartyAddress": {
                "FirstName": "John",
                "SureName": "Doe",
                "AddressLine1": "6541 Hollywood Blvd",
                "ZipCode": "90028",
                "City": "Los Angeles",
                "Country": "USA",
                "CountryCode": "US"
            },
            "BuyerPartyInfo": {
                "CustomerNumber": "88888888888",
                "MobilePhoneNumber": "55555555555",
                "EmailAddress": "john@doe.com"
            }
        },
        "InvoiceInfo": {
            "IssueDate": "string",
            "DueDate": "string",
            "InvoiceNumber": "string",
            "PaymentTerms": "string",
            "SellerRef": "string",
            "BuyerRef": "string",
            "PaymentRef": "string",
            "Currency": "string",
            "PBSnumber": "string",
            "DebGrNr": "string",
            "Transactions": {
                "TransactionLine": {
                    "ArtNo": "123",
                    "Description": "Something",
                    "QTY": "2",
                    "Unit": "st",
                    "NetPrice": "200",
                    "VATRate": "25",
                    "AmountExVAT": "400"
                }
            },
            "TotalAmoutExVAT": "string",
            "TotalPayableAmount": "string",
            "TotalVAT": {
                "VATSubtotal": {
                    "Percent": "25",
                    "VATAmount": "100",
                    "AmountExVAT": "400"
                }
            }
        }
    },
    "Invoice": {
        "SellerParty": {
            "SellerPartyAddress": {
                "Name": "The Company222",
                "AddressLine1": "Street",
                "AddressLine2": "Box 111",
                "ZipCode": "123456",
                "City": "STHLM",
                "Country": "Sweden",
                "CountryCode": "SE"
            },
            "SellerPartyInfo": {
                "WebAddress": "www.thecompany.com",
                "PhoneNumber": "123456789",
                "EmailAddress": "info@thecompany.com"
            },
            "SellerPartyPaymentMeans": {
                "IBAN": "123455670",
                "BICSWIFT": "000000000000",
                "BankAccount": "1111111111111"
            }
        },
        "BuyerParty": {
            "BuyerPartyAddress": {
                "FirstName": "Jane",
                "SureName": "Doe",
                "AddressLine1": "6541 Hollywood Blvd",
                "ZipCode": "90028",
                "City": "Los Angeles",
                "Country": "USA",
                "CountryCode": "US"
            },
            "BuyerPartyInfo": {
                "CustomerNumber": "88888888888",
                "MobilePhoneNumber": "55555555555",
                "EmailAddress": "jane@doe.com"
            }
        },
        "InvoiceInfo": {
            "IssueDate": "string",
            "DueDate": "string",
            "InvoiceNumber": "string",
            "PaymentTerms": "string",
            "SellerRef": "string",
            "BuyerRef": "string",
            "PaymentRef": "string",
            "Currency": "string",
            "Transactions": {
                "TransactionLine": {
                    "ArtNo": "123",
                    "Description": "Something",
                    "QTY": "2",
                    "Unit": "st",
                    "NetPrice": "200",
                    "VATRate": "25",
                    "AmountExVAT": "400"
                }
            },
            "TotalAmoutExVAT": "string",
            "TotalPayableAmount": "string",
            "TotalVAT": {
                "VATSubtotal": {
                    "Percent": "25",
                    "VATAmount": "100",
                    "AmountExVAT": "400"
                }
            }
        }
    }
}
}

【问题讨论】:

  • 由于您没有上传代码 sn-p,我现在不知道您的 API 端点看起来如何。如果它得到字符串Post(string invoices),你可以使用JArray.Parse类似:JArray invoices = JArray.Parse(invoices); foreach (var invoice in invoices) { //do your job }

标签: c# json api


【解决方案1】:

您不能在 JSON 对象的同一级别上拥有多个同名的属性。在您的代码中,您有多个名为“Invoice”的属性,这会导致问题。

将发票作为 JSON 数组可以解决无效 JSON 的问题。即:

{
    "Invoices": [
        {
            "SellerParty": {
                "SellerPartyAddress": {
                    "Name": "The Company111",
                    "AddressLine1": "Street",
                    "AddressLine2": "Box 111",
                    "ZipCode": "123456",
                    "City": "STHLM",
                    "Country": "Sweden",
                    "CountryCode": "SE"
                },
                "SellerPartyInfo": {
                    "WebAddress": "www.thecompany.com",
                    "PhoneNumber": "123456789",
                    "EmailAddress": "info@thecompany.com"
                },
                "SellerPartyPaymentMeans": {
                    "IBAN": "123455670",
                    "BICSWIFT": "000000000000",
                    "BankAccount": "1111111111111"
                }
            },
            "BuyerParty": {
                "BuyerPartyAddress": {
                    "FirstName": "John",
                    "SureName": "Doe",
                    "AddressLine1": "6541 Hollywood Blvd",
                    "ZipCode": "90028",
                    "City": "Los Angeles",
                    "Country": "USA",
                    "CountryCode": "US"
                },
                "BuyerPartyInfo": {
                    "CustomerNumber": "88888888888",
                    "MobilePhoneNumber": "55555555555",
                    "EmailAddress": "john@doe.com"
                }
            },
            "InvoiceInfo": {
                "IssueDate": "string",
                "DueDate": "string",
                "InvoiceNumber": "string",
                "PaymentTerms": "string",
                "SellerRef": "string",
                "BuyerRef": "string",
                "PaymentRef": "string",
                "Currency": "string",
                "PBSnumber": "string",
                "DebGrNr": "string",
                "Transactions": {
                    "TransactionLine": {
                        "ArtNo": "123",
                        "Description": "Something",
                        "QTY": "2",
                        "Unit": "st",
                        "NetPrice": "200",
                        "VATRate": "25",
                        "AmountExVAT": "400"
                    }
                },
                "TotalAmoutExVAT": "string",
                "TotalPayableAmount": "string",
                "TotalVAT": {
                    "VATSubtotal": {
                        "Percent": "25",
                        "VATAmount": "100",
                        "AmountExVAT": "400"
                    }
                }
            }
        },
        {
            "SellerParty": {
                "SellerPartyAddress": {
                    "Name": "The Company222",
                    "AddressLine1": "Street",
                    "AddressLine2": "Box 111",
                    "ZipCode": "123456",
                    "City": "STHLM",
                    "Country": "Sweden",
                    "CountryCode": "SE"
                },
                "SellerPartyInfo": {
                    "WebAddress": "www.thecompany.com",
                    "PhoneNumber": "123456789",
                    "EmailAddress": "info@thecompany.com"
                },
                "SellerPartyPaymentMeans": {
                    "IBAN": "123455670",
                    "BICSWIFT": "000000000000",
                    "BankAccount": "1111111111111"
                }
            },
            "BuyerParty": {
                "BuyerPartyAddress": {
                    "FirstName": "Jane",
                    "SureName": "Doe",
                    "AddressLine1": "6541 Hollywood Blvd",
                    "ZipCode": "90028",
                    "City": "Los Angeles",
                    "Country": "USA",
                    "CountryCode": "US"
                },
                "BuyerPartyInfo": {
                    "CustomerNumber": "88888888888",
                    "MobilePhoneNumber": "55555555555",
                    "EmailAddress": "jane@doe.com"
                }
            },
            "InvoiceInfo": {
                "IssueDate": "string",
                "DueDate": "string",
                "InvoiceNumber": "string",
                "PaymentTerms": "string",
                "SellerRef": "string",
                "BuyerRef": "string",
                "PaymentRef": "string",
                "Currency": "string",
                "Transactions": {
                    "TransactionLine": {
                        "ArtNo": "123",
                        "Description": "Something",
                        "QTY": "2",
                        "Unit": "st",
                        "NetPrice": "200",
                        "VATRate": "25",
                        "AmountExVAT": "400"
                    }
                },
                "TotalAmoutExVAT": "string",
                "TotalPayableAmount": "string",
                "TotalVAT": {
                    "VATSubtotal": {
                        "Percent": "25",
                        "VATAmount": "100",
                        "AmountExVAT": "400"
                    }
                }
            }
        }
    ]
}

然后您需要遍历数组中的项目来处理每个项目。

附:网站https://jsonlint.com 在检查您的 JSON 是否有效方面非常有用

【讨论】:

  • 编辑了我的答案以澄清该数组将修复无效的 json,后端可能需要更多的工作来实际处理每个数组项。
猜你喜欢
  • 1970-01-01
  • 2020-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多