Home > Net >  vbdotnet Deserialize json to a class
vbdotnet Deserialize json to a class

Time:08-16

I am getting the below exception when deserializing a simple json text to a class. please help.

"Exception thrown: 'Newtonsoft.Json.JsonReaderException' in Newtonsoft.Json.dll An unhandled exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll Unexpected character encountered while parsing value: [. Path 'Array1', line 4, position 14."

Below is the simple json text

{
    "Name1": "Value1",
    "Name2": "Value2",  
    "Array1":["a", "b", "c", "d"]
}

and below is the Class/Model

Public Class Test
    Public Property Name1 As String
    Public Property Name2 As String
    Public Property Array1() As String
End Class

and below is the main module

Imports System
Imports System.IO
Imports System.Collections.Generic
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Serialization
Imports Newtonsoft.Json.Converters
Imports Newtonsoft.Json.Linq
Module Module1

    Sub Main()
        
       'Here I am reading all text from the json file to "strJsonText" and then the below line throws 'exception

        Dim classDocs = JsonConvert.DeserializeObject(strJsonText, GetType(Test))

    End Sub

End Module

CodePudding user response:

You are declaring this property wrong, it is actually a String type not an array, and it has an empty parameter section.

Public Property Array1() As String

It should be an array of strings

Public Property Array1 As String()

Furthermore, you should use the generic version of DeserializeObject

Dim classDocs = JsonConvert.DeserializeObject(Of Test)(strJsonText)

dotnetfiddle

CodePudding user response:

Visual Studio has a cool feature called Paste JSON as Classes that can be found under Edit > Paste Special > Paste JSON as Classes. Using this feature produces the same results you have, I wanted to share this with you because it is a nice feature to know about nonetheless.


With regards to your actual problem, the issue is where the parenthesis are in your array declaration. In Visual Basic .NET, the parenthesis are implicit in property definitions. That means that these two property definitions are functionally equivalent:

Public Property Name1() As String
' or
Public Property Name1 As String

This gets a bit confusing because an array variable can be declared as such:

Dim Array1() As String

But when you try to convert this to a property, you have to move the parenthesis to the end to compensate for the parenthesis in property definitions.

What I would suggest doing is changing your array definition to an IEnumerable instead and then explicitly cast it to an array if you need to. In other words, something like this:

Public Class Test
    Public Property Name1 As String
    Public Property Name2 As String
    Public Property Array1 As IEnumerable(Of String)

    Public Shared Function ParseJson(literal As String)
        Return JsonConvert.DeserializeObject(Of Test)(literal)
    End Function
End Class

Example: https://dotnetfiddle.net/DVgpI3

CodePudding user response:

You need to add Test class in your Json so you can deserialize

'Test': [
  {
    'Name1': 'Value1',
    'Name2': 'Value2',
    'Array1': [
      'a',
      'b',
      'c',
      'd'
    ]
  ]
}
  • Related