Can't get over this error, can someone please help.
Error: Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Models'.
JSON (Validated):
[
{
"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 Classes
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
And my code:
Dim modelList As Models = JsonConvert.DeserializeObject(jsonString)
Where am I going wrong?
Cheers.
You should specify the type you want to deserialize to in the call, as a generic type argument:
Dim modelList As Models = JsonConvert.DeserializeObject(Of Models)(jsonString)
Although you may find that doesn't work, and that you actually need to specify that you want a List(Of Model)
, given that your JSON is an array rather than a single object:
Dim modelList = JsonConvert.DeserializeObject(Of List(Of Model))(jsonString)
See more on this question at Stackoverflow