Skip to main content
Commonmark migration
Source Link

###Here's a working dotNetFiddle: https://dotnetfiddle.net/81Huv6

Here's a working dotNetFiddle: https://dotnetfiddle.net/81Huv6

Here's the output of the Deserialization.

###Here's the output of the Deserialization. how to deserialize json vb.net

###Here's a working dotNetFiddle: https://dotnetfiddle.net/81Huv6

###Here's the output of the Deserialization. how to deserialize json vb.net

Here's a working dotNetFiddle: https://dotnetfiddle.net/81Huv6

Here's the output of the Deserialization.

how to deserialize json vb.net

added 1754 characters in body
Source Link
Shiva
  • 21.1k
  • 15
  • 88
  • 119

###Here's the output of the Deserialization. how to deserialize json vb.net

You have to deserialize the JSON into an Array of Vars. Like So.

Dim varses() = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Vars())(rawresp)

Notice also that the literal JSON string has the name and value strings enclosed in double quotes. You were probably getting invalid JSON format error due to the missing extra quote around each name and each value.

rawresp = "[{""Var1"":""data1"",""Var2"":""data2"",""Var3"":""data3"",""Var4"":""data4""}]"

Also be sure to check for empty array etc before attempting to access the items inside.

Here's the entire code listing that works.

Imports System
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

    ' Author: Shiva Manjunath
    ' Author's Stackoverflow Profile: http://stackoverflow.com/users/325521/shiva
Public Module Module1
    Public Sub Main()
        Dim rawresp As String
        rawresp = "[{""Var1"":""data1"",""Var2"":""data2"",""Var3"":""data3"",""Var4"":""data4""}]"

        Console.WriteLine("Raw JSON : " + rawresp)
        Console.WriteLine()
        
        Console.WriteLine("---BEGIN JSON Deserialization")
        Dim varses() = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Vars())(rawresp)

        ' Loop over each Var in the Array of Vars.      
        For Each oneVar As Vars In varses
            ' Avoid Nothing vars.
            If oneVar IsNot Nothing Then
                Console.WriteLine("    Var1 = " + oneVar.Var1)
                Console.WriteLine("    Var2 = " + oneVar.Var2)
                Console.WriteLine("    Var3 = " + oneVar.Var3)
                Console.WriteLine("    Var4 = " + oneVar.Var4)
            End If
        Next

        Console.WriteLine("---END JSON Deserialization")

    End Sub
End Module

Public Class Vars
   Public Property Var1 As String
   Public Property Var2 As String
   Public Property Var3 As String
   Public Property Var4 As String
End Class

###Here's the output of the Deserialization. how to deserialize json vb.net

You have to deserialize the JSON into an Array of Vars. Like So.

Dim varses() = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Vars())(rawresp)

Notice also that the literal JSON string has the name and value strings enclosed in double quotes. You were probably getting invalid JSON format error due to the missing extra quote around each name and each value.

rawresp = "[{""Var1"":""data1"",""Var2"":""data2"",""Var3"":""data3"",""Var4"":""data4""}]"

Also be sure to check for empty array etc before attempting to access the items inside.

Here's the entire code listing that works.

Imports System
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

    ' Author: Shiva Manjunath
    ' Author's Stackoverflow Profile: http://stackoverflow.com/users/325521/shiva
Public Module Module1
    Public Sub Main()
        Dim rawresp As String
        rawresp = "[{""Var1"":""data1"",""Var2"":""data2"",""Var3"":""data3"",""Var4"":""data4""}]"

        Console.WriteLine("Raw JSON : " + rawresp)
        Console.WriteLine()
        
        Console.WriteLine("---BEGIN JSON Deserialization")
        Dim varses() = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Vars())(rawresp)

        ' Loop over each Var in the Array of Vars.      
        For Each oneVar As Vars In varses
            ' Avoid Nothing vars.
            If oneVar IsNot Nothing Then
                Console.WriteLine("    Var1 = " + oneVar.Var1)
                Console.WriteLine("    Var2 = " + oneVar.Var2)
                Console.WriteLine("    Var3 = " + oneVar.Var3)
                Console.WriteLine("    Var4 = " + oneVar.Var4)
            End If
        Next

        Console.WriteLine("---END JSON Deserialization")

    End Sub
End Module

Public Class Vars
   Public Property Var1 As String
   Public Property Var2 As String
   Public Property Var3 As String
   Public Property Var4 As String
End Class
Source Link
Shiva
  • 21.1k
  • 15
  • 88
  • 119

###Here's a working dotNetFiddle: https://dotnetfiddle.net/81Huv6

Wait a few seconds for the code to run, and then look at the output in the Console Window (bottom pane on that page).