我试图反序列化JSON,但Newtonsoft.Json抛出此错误

时间:2016-06-10 21:32:17

标签: json vb.net

  

无法将当前的JSON对象(例如{" name":" value"})反序列化为类型' Retail_Web.Helpers + Sale1 []'因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化。
  要修复此错误,请将JSON更改为JSON数组(例如[1,2,3])或更改反序列化类型,使其成为普通的.NET类型(例如,不是像整数这样的基本类型,而不是类似的集合类型可以从JSON对象反序列化的数组或List。 JsonObjectAttribute也可以添加到类型中以强制它从JSON对象反序列化。
  路径' Sale.saleID',第1行,第73位。

JSON:

{
    "Sale": {
        "saleID": "54",
        "timeStamp": "2016-06-10T13:03:16+00:00",
        "discountPercent": "0"
    }
}

型号:

Public Class Helpers
    Public Property Sale As Sales()

    Public Class Sales 
        Public Property saleID As String
        Public Property timeStamp As String
        Public Property discountPercent As String
        Public Property completed As String
    End Class
End Class

而且,这是导致错误的代码:

Dim OrdersReceiptList2 As Helpers

OrdersReceiptList2 = JsonConvert.DeserializeObject(Of Helpers)(File.ReadAllText("c:\test.json"))
MsgBox(OrdersReceiptList2.Sale(0).saleID)

1 个答案:

答案 0 :(得分:0)

Sale不是数组。试试这个:

Public Class Helpers
    Public Property Sale As Sales 'removed ()

    Public Class Sales 
        Public Property saleID As String
        Public Property timeStamp As String
        Public Property discountPercent As String
        Public Property completed As String
    End Class
End Class
MsgBox(OrdersReceiptList2.Sale.saleID)