请帮助!!!! 我试图反序列化一个Json。我遇到的问题是当我只有1个“SalePayment”时(因为这不是作为数组出现的)。如果我在示例json中有几个“SalePayment”,那么我的代码工作正常。
这是json
{
"@attributes": {
"count": "1",
"offset": "0",
"limit": "100"
},
"Sale": {
"saleID": "55",
"timeStamp": "2016-06-11T13:08:16+00:00",
"discountPercent": "0",
"completed": "true",
"archived": "false",
"voided": "false",
"enablePromotions": "true",
"isTaxInclusive": "false",
"referenceNumber": "",
"referenceNumberSource": "",
"tax1Rate": "0",
"tax2Rate": "0",
"change": "0",
"receiptPreference": "printed",
"displayableSubtotal": "2700",
"ticketNumber": "220000000055",
"calcDiscount": "0",
"calcTotal": "2700",
"calcSubtotal": "2700",
"calcTaxable": "0",
"calcNonTaxable": "2700",
"calcAvgCost": "0",
"calcFIFOCost": "0",
"calcTax1": "0",
"calcTax2": "0",
"calcPayments": "2700",
"total": "2700",
"totalDue": "2700",
"displayableTotal": "2700",
"balance": "0",
"customerID": "0",
"discountID": "0",
"employeeID": "1",
"quoteID": "0",
"registerID": "1",
"shipToID": "0",
"shopID": "1",
"taxCategoryID": "0",
"SalePayments": {
"SalePayment": [
{
"salePaymentID": "40",
"amount": "500",
"createTime": "2016-06-11T13:08:16+00:00",
"archived": "false",
"remoteReference": "",
"paymentID": "",
"saleID": "55",
"paymentTypeID": "2",
"ccChargeID": "0",
"refPaymentID": "0",
"registerID": "1",
"employeeID": "1",
"creditAccountID": "0",
"PaymentType": {
"paymentTypeID": "2",
"name": "Check",
"requireCustomer": "false",
"archived": "false",
"internalReserved": "false",
"type": "user defined",
"refundAsPaymentTypeID": "0"
}
},
{
"salePaymentID": "41",
"amount": "500",
"createTime": "2016-06-11T13:08:16+00:00",
"archived": "false",
"remoteReference": "",
"paymentID": "",
"saleID": "55",
"paymentTypeID": "3",
"ccChargeID": "0",
"refPaymentID": "0",
"registerID": "1",
"employeeID": "1",
"creditAccountID": "0",
"PaymentType": {
"paymentTypeID": "3",
"name": "Credit Card",
"requireCustomer": "false",
"archived": "false",
"internalReserved": "false",
"type": "user defined",
"refundAsPaymentTypeID": "0"
}
},
{
"salePaymentID": "42",
"amount": "1300",
"createTime": "2016-06-11T13:08:16+00:00",
"archived": "false",
"remoteReference": "",
"paymentID": "",
"saleID": "55",
"paymentTypeID": "6",
"ccChargeID": "0",
"refPaymentID": "0",
"registerID": "1",
"employeeID": "1",
"creditAccountID": "0",
"PaymentType": {
"paymentTypeID": "6",
"name": "Debit Card",
"requireCustomer": "false",
"archived": "false",
"internalReserved": "false",
"type": "user defined",
"refundAsPaymentTypeID": "0"
}
},
{
"salePaymentID": "43",
"amount": "400",
"createTime": "2016-06-11T13:08:16+00:00",
"archived": "false",
"remoteReference": "",
"paymentID": "",
"saleID": "55",
"paymentTypeID": "1",
"ccChargeID": "0",
"refPaymentID": "0",
"registerID": "1",
"employeeID": "1",
"creditAccountID": "0",
"PaymentType": {
"paymentTypeID": "1",
"name": "Cash",
"requireCustomer": "false",
"archived": "false",
"internalReserved": "true",
"type": "cash",
"refundAsPaymentTypeID": "0"
}
}
]
},
"taxTotal": "0"
}
}
这是CLASS 公共级助手
Public Property attribute As Attributes
Public Property Sale As Sales
Public Class Attributes
Public Property count As String
Public Property offset As String
Public Property limit As String
End Class
Public Class PaymentType
Public Property paymentTypeID As String
Public Property name As String
Public Property requireCustomer As String
Public Property archived As String
Public Property internalReserved As String
Public Property type As String
Public Property refundAsPaymentTypeID As String
End Class
Public Class SalePayment
Public Property salePaymentID As String
Public Property amount As String
Public Property createTime As DateTime
Public Property archived As String
Public Property remoteReference As String
Public Property paymentID As String
Public Property saleID As String
Public Property paymentTypeID As String
Public Property ccChargeID As String
Public Property refPaymentID As String
Public Property registerID As String
Public Property employeeID As String
Public Property creditAccountID As String
Public Property PaymentType As PaymentType
End Class
Public Class SalePayments
Public Property SalePayment As SalePayment()
End Class
Public Class Sales
Public Property saleID As String
Public Property timeStamp As DateTime
Public Property discountPercent As String
Public Property completed As String
Public Property archived As String
Public Property voided As String
Public Property enablePromotions As String
Public Property isTaxInclusive As String
Public Property referenceNumber As String
Public Property referenceNumberSource As String
Public Property tax1Rate As String
Public Property tax2Rate As String
Public Property change As String
Public Property receiptPreference As String
Public Property displayableSubtotal As String
Public Property ticketNumber As String
Public Property calcDiscount As String
Public Property calcTotal As String
Public Property calcSubtotal As String
Public Property calcTaxable As String
Public Property calcNonTaxable As String
Public Property calcAvgCost As String
Public Property calcFIFOCost As String
Public Property calcTax1 As String
Public Property calcTax2 As String
Public Property calcPayments As String
Public Property total As String
Public Property totalDue As String
Public Property displayableTotal As String
Public Property balance As String
Public Property customerID As String
Public Property discountID As String
Public Property employeeID As String
Public Property quoteID As String
Public Property registerID As String
Public Property shipToID As String
Public Property shopID As String
Public Property taxCategoryID As String
Public Property SalePayments As SalePayments
Public Property taxTotal As String
End Class
End Class
这是代码
Dim OrdersSaleList As Helpers
OrdersSaleList = JsonConvert.DeserializeObject(Of Helpers)(json)
我收到此错误
无法反序列化当前的JSON对象(例如{“name”:“value”}) 进入类型'WindowsApplication1.Helpers + SalePayment []'因为类型 需要一个JSON数组(例如[1,2,3])才能正确反序列化。修理 此错误要么将JSON更改为JSON数组(例如[1,2,3])或 更改反序列化类型,使其成为正常的.NET类型(例如 不是像整数这样的原始类型,不是像数组那样的集合类型 或者List)可以从JSON对象反序列化。 JsonObjectAttribute也可以添加到类型中以强制它 从JSON对象反序列化。路径 'Sale.SalePayments.SalePayment.salePaymentID',第1行,第3821位。