1

I am revisiting VB.NET after years of not doing anything with it. SO take it a little easy on me, also I have googled this and searched through Stackoverflow and have not found something to help.

I have this JSON coming in from a web API.

Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

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

I have tried this way of doing it

Dim json As JObject = JObject.Parse(rawresp)

and I have tried this way

Dim obj = JsonConvert.DeserializeObject(Of Vars)(rawresp)

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

I am really having trouble seeing that deserializing json is this difficult, I must be missing something basic.

0

3 Answers 3

2

You are parsing array as object try to remove "[" and "]" from the json string you have to do it like this

    Try
        Dim rawresp As String = "{'name':'hassan','age':24,'sex':'male'}"
        Dim json As JObject = JObject.Parse(rawresp)
        MsgBox(json.Item("name"))
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
Sign up to request clarification or add additional context in comments.

Comments

1

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).

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

1 Comment

Thank you, your answer was dead on and has been accepted.
0

The problem is that your JSON represents an array (of length 1) of dictionaries. To deserialize this in C# (sorry I am not very familiar with VB.Net):

var obj = JsonConvert.DeserializeObject<Dictionary<string, string>[]>(rawresp);

obj will be an array containing a single dictionary with the 4 name-value-pairs shown in the JSON.

2 Comments

Or just deserialize into an array of Vars
@AndrewWhitaker Yeah, I realized this on my drive home :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.