C# 如何使用多个JSON对象处理对API的POST请求

C# 如何使用多个JSON对象处理对API的POST请求,c#,json,api,C#,Json,Api,我有一个API,需要通过POST请求接收多张发票。传入的JSON如下所示。 我的模型看起来就像JSON对象 如果我寄一张发票,一切正常,加工正常。如果像示例一样发送两张发票,我只会获取第二张发票的数据,我假设这是因为这是最后一张要反序列化的发票。是否有一种方法可以让我循环查看发票并处理每张发票 我为自己是C和API开发的初学者而道歉 { "Invoices": { "Invoice": { "SellerParty": { "SellerParty

我有一个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"
                }
            }
        }
    }
}
}

在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"
                    }
                }
            }
        }
    ]
}
然后需要遍历数组中的项来处理每个项


另外,该网站在检查您的JSON是否有效方面是非常宝贵的

编辑了我的回答,以澄清数组将修复无效的JSON,可能需要在后端进行更多的工作来实际处理每个数组项。由于您没有上传代码片段,我现在不知道您的API端点外观如何。如果它获取字符串Poststring invoices,您可以使用JArray.Parse,例如:JArray invoices=JArray.Parseinvoices;发票中的foreach var发票{//完成您的工作}