DeserializeObject()上的JSON.NET强制转换异常

时间:2015-10-02 14:56:24

标签: json vb.net linq

无法克服此错误,有人可以帮忙。

错误:无法转换类型' Newtonsoft.Json.Linq.JObject'键入' Models'。

JSON(已验证):

[
{
    "ModelID": 5172,
    "CatalogueID": 11364,
    "MODEL": "CRF250RG",
    "MODELNAME": "CRF250R",
    "TYPE": "U",
    "CC": "250",
    "YEAR": "2016",
    "Notes": null,
    "QTYCOL": 5,
    "Duplicate": false,
    "Superceeded": false,
    "dateModified": "/Date(1442722515797)/",
    "dateRelease": "/Date(1442757600000)/",
    "IncUpdateEnabled": true,
    "Category": 0,
    "OnlineEnabled": true
}
]

VB.NET类

    Public Class Models
    Public Property Models() As List(Of Model)
        Get
            Return m_models
        End Get
        Set(value As List(Of Model))
            m_models = value
        End Set
    End Property
    Private m_models As List(Of Model)
End Class

Public Class Model
    Public Property ModelID() As Integer
        Get
            Return m_ModelID
        End Get
        Set(value As Integer)
            m_ModelID = value
        End Set
    End Property
    Private m_ModelID As Integer
    Public Property CatalogueID() As Integer
        Get
            Return m_CatalogueID
        End Get
        Set(value As Integer)
            m_CatalogueID = value
        End Set
    End Property
    Private m_CatalogueID As Integer
    Public Property MODEL() As String
        Get
            Return m_MODEL
        End Get
        Set(value As String)
            m_MODEL = value
        End Set
    End Property
    Private m_MODEL As String
    Public Property MODELNAME() As String
        Get
            Return m_MODELNAME
        End Get
        Set(value As String)
            m_MODELNAME = value
        End Set
    End Property
    Private m_MODELNAME As String
    Public Property TYPE() As String
        Get
            Return m_TYPE
        End Get
        Set(value As String)
            m_TYPE = value
        End Set
    End Property
    Private m_TYPE As String
    Public Property CC() As String
        Get
            Return m_CC
        End Get
        Set(value As String)
            m_CC = value
        End Set
    End Property
    Private m_CC As String
    Public Property YEAR() As String
        Get
            Return m_YEAR
        End Get
        Set(value As String)
            m_YEAR = value
        End Set
    End Property
    Private m_YEAR As String
    Public Property Notes() As Object
        Get
            Return m_Notes
        End Get
        Set(value As Object)
            m_Notes = value
        End Set
    End Property
    Private m_Notes As Object
    Public Property QTYCOL() As Integer
        Get
            Return m_QTYCOL
        End Get
        Set(value As Integer)
            m_QTYCOL = value
        End Set
    End Property
    Private m_QTYCOL As Integer
    Public Property Duplicate() As Boolean
        Get
            Return m_Duplicate
        End Get
        Set(value As Boolean)
            m_Duplicate = value
        End Set
    End Property
    Private m_Duplicate As Boolean
    Public Property Superceeded() As Boolean
        Get
            Return m_Superceeded
        End Get
        Set(value As Boolean)
            m_Superceeded = value
        End Set
    End Property
    Private m_Superceeded As Boolean
    Public Property dateModified() As DateTime
        Get
            Return m_dateModified
        End Get
        Set(value As DateTime)
            m_dateModified = value
        End Set
    End Property
    Private m_dateModified As DateTime
    Public Property dateRelease() As DateTime
        Get
            Return m_dateRelease
        End Get
        Set(value As DateTime)
            m_dateRelease = value
        End Set
    End Property
    Private m_dateRelease As DateTime
    Public Property IncUpdateEnabled() As Boolean
        Get
            Return m_IncUpdateEnabled
        End Get
        Set(value As Boolean)
            m_IncUpdateEnabled = value
        End Set
    End Property
    Private m_IncUpdateEnabled As Boolean
    Public Property Category() As Integer
        Get
            Return m_Category
        End Get
        Set(value As Integer)
            m_Category = value
        End Set
    End Property
    Private m_Category As Integer
    Public Property OnlineEnabled() As Boolean
        Get
            Return m_OnlineEnabled
        End Get
        Set(value As Boolean)
            m_OnlineEnabled = value
        End Set
    End Property
    Private m_OnlineEnabled As Boolean
End Class

我的代码:

Dim modelList As Models = JsonConvert.DeserializeObject(jsonString)

我哪里错了?

干杯。

1 个答案:

答案 0 :(得分:2)

您应该在调用中指定要反序列化的类型,作为泛型类型参数:

Dim modelList As Models = JsonConvert.DeserializeObject(Of Models)(jsonString)

虽然你可能发现它不起作用,并且你实际上需要指定你想要List(Of Model),因为你的JSON是一个数组而不是一个对象:

Dim modelList = JsonConvert.DeserializeObject(Of List(Of Model))(jsonString)