Home > Software design >  JSON.Net Deserialization error, My Class definition is wrong?
JSON.Net Deserialization error, My Class definition is wrong?

Time:07-22

In VB.Net program I'm getting the "Cannot deserialize the current JSON array" when using JsonConvert.DeserializeObject(Of MCMusicElements)(sRB).

The JSON array I'm working with is: [{"Key":465419,"MIK_Energy":3,"MIK_Camelot":"9B","MIK_BPM":118}]

If I delete the "[ ]" in the JSON string my program works. So, I'm assuming my Class definition for MCMusicElements is wrong in some way. I'd like to understand how to make it work without deleting the brackets from the JSON string.

Public Class MCMusicElements
  Public Property Key As Integer
  Public Property MIK_Energy As Integer
  Public Property MIK_Camelot As String
  Public Property MIK_BPM As Integer
End Class

Dim oResult = JsonConvert.DeserializeObject(Of MCMusicElements)(sRB)
dtDataTable.Rows(index).Item(1) = oResult.MIK_Energy
dtDataTable.Rows(index).Item(2) = oResult.MIK_Camelot
dtDataTable.Rows(index).Item(3) = oResult.MIK_BPM

CodePudding user response:

you have to use list of objects, not an object

Dim oResult = JsonConvert.DeserializeObject(Of List(Of MCMusicElements))(sRB)

dtDataTable.Rows(index).Item(1) = oResult(0).MIK_Energy
  • Related