Ravi Kiran
Ravi Kiran

Reputation: 585

JSON.Net Deserialization in VB .Net

I want to fetch data from a website and populate some of the fields in my application's listview. I read few articles and I'm confused to choose between JavaScriptSerializer and JSON.Net. What is the best way to solve this?

Here is the JSON response from website

{
"variations": {
"variants": [
    {"id": "AQ5929_580",
    "avStatusQuantity": 1.0,
    "avStatus": "NOT_AVAILABLE",
    "inStock": false,
    "avLevels": {"IN_STOCK": 0.0,
    "PREORDER": 0.0,
    "BACKORDER": 0.0,
    "NOT_AVAILABLE": 1.0,
    "PREVIEW": 1.0},
    "ATS": 0.0,
    "inStockDate": "Tue Jun 14 00:00:00 GMT 2016"
    }
    ,

    {"id": "AQ5929_590",
    "avStatusQuantity": 1.0,
    "avStatus": "NOT_AVAILABLE",
    "inStock": false,
    "avLevels": {"IN_STOCK": 0.0,
    "PREORDER": 0.0,
    "BACKORDER": 0.0,
    "NOT_AVAILABLE": 1.0,
    "PREVIEW": 1.0},
    "ATS": 0.0,
    "inStockDate": "Tue Jun 14 00:00:00 GMT 2016"
    }
    ,

    {"id": "AQ5929_600",
    "avStatusQuantity": 1.0,
    "avStatus": "NOT_AVAILABLE",
    "inStock": false,
    "avLevels": {"IN_STOCK": 0.0,
    "PREORDER": 0.0,
    "BACKORDER": 0.0,
    "NOT_AVAILABLE": 1.0,
    "PREVIEW": 1.0},
    "ATS": 0.0,
    "inStockDate": "Tue Jun 14 00:00:00 GMT 2016"
    }
    ]
    }
}

I want only the values of id, inStock, ATS fields.

My VB .Net code so far,

<Serializable> _
Public Class Variants
    Private _id As String = Nothing
    Public Property id() As String
        Get
            Return _id
        End Get
        Set(value As String)
            _id = value
        End Set
    End Property

    Private _avStatusQuantity As Double
    Public Property avStatusQuantity() As Double
        Get
            Return _avStatusQuantity
        End Get
        Set(value As Double)
            _avStatusQuantity = value
        End Set
    End Property

    Private _avStatus As String = Nothing
    Public Property avStatus() As String
        Get
            Return _avStatus
        End Get
        Set(value As String)
            _avStatus = value
        End Set
    End Property

    Private _inStock As Boolean = False
    Public Property inStock() As Boolean
        Get
            Return _inStock
        End Get
        Set(value As Boolean)
            _inStock = value
        End Set
    End Property

    Private _ATS As String = Nothing
    Public Property ATS() As Double
        Get
            Return _ATS
        End Get
        Set(value As Double)
            _ATS = value
        End Set
    End Property

    Private _inStockDate As String = Nothing
    Public Property inStockDate() As String
        Get
            Return _inStockDate
        End Get
        Set(value As String)
            _inStockDate = value
        End Set
    End Property
End Class

<Serializable> _
Public Class Variations
    Private _Variants As Variants
    Public Property variants As Variants
        Get
            Return _Variants
        End Get
        Set(value As Variants)
            _Variants = value
        End Set
    End Property
End Class

When I use JSONConvert function for deserializing, result is always nothing

Dim x = JsonConvert.DeserializeObject(Of Variants)(Response)
MessageBox.Show(x.id)

I've looked for more solutions but most of them are in C# and they confuse me. If anyone can help me, Thanks!

Upvotes: 1

Views: 3275

Answers (2)

There are 2 other "things" in the json besides the part you want. You could think of the part you want being: Container.variations.variant(N). You can see the "variations {}" wrapping what you want in the json, then the outermost { .. } is the object which was serialized, which is another class to contend with. So:

Public Class VariationsContainer
    Public Property variations As Variations
End Class
Public Class Variations
    Public Property variants As MyVariant()
End Class

Public Class MyVariant
    Public Property id As String
    Public Property ATS As Double
    Public Property inStockDate As String
End Class

Variant is a keyword in VB, so I used MyVariant for the class, the property names have to be as used in the json to avoid the use of JProperty. Then:

Dim jstr As String = from whereever

Dim jData = JsonConvert.DeserializeObject(Of VariationsContainer)(jstr)
Console.WriteLine(jData.variations.variants(0).id)

AQ5929_580


If you dont want to define those 2 other classes, 1-2 more steps will get rid of them and the obj.variations.Variants() notation everywhere:

Dim jobj = JObject.Parse(jstr)
Dim vars = JsonConvert.DeserializeObject(Of List(Of MyVariant))(jobj("variations")("variants").ToString)
Console.WriteLine(vars(1).id)

AQ5929_590

This results in a List(Of MyVariants) use DeserializeObject(Of MyVariant()) to get an array back instead.

I'm confused to choose between JavaScriptSerializer and JSON.Net The first words on MSDN for JavaScriptSerializer Class:

Json.NET should be used serialization and deserialization.[sic]

Upvotes: 2

Paul Swetz
Paul Swetz

Reputation: 2254

I'm not sure the exact VB but the response as shown is a Collection Of Variants not a single variant.

You may need something closer to

Dim x = JsonConvert.DeserializeObject(Of IEnumerable(Variants))(Response)
For Each v as Variant In x
//do stuff with each one.
Next

Upvotes: 0

Related Questions