VB.net JSON Deserialize

Here is the easiest way to deserialize JSON into an object (using .NET 4):

Example JSON:

{
    "dogs":[],
    "chickens":[
        {
            "name":"Macey",
            "eggs":7
        }, 
        {
            "name":"Alfred",
            "eggs":2
        }
    ]
}

VB.NET:

Try
    Dim j As Object = New JavaScriptSerializer().Deserialize(Of Object)(JSONString)
    Dim a = j("dogs")                   ' returns empty Object() array
    Dim b = j("chickens")(0)            ' returns Dictionary(Of String, Object)
    Dim c = j("chickens")(0)("name")    ' returns String "Macey"
    Dim d = j("chickens")(1)("eggs")    ' returns Integer 2
Catch ex As Exception
    ' in case the structure of the object is not what we expected.
End Try

Leave a Comment