【发布时间】: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 }