Note: JSON serialization is the process of "converting JSON object into JSON string".
JSON deserialization is process of "converting JSON data into .Net objects".
I am using the deserialization method in my example here to convert JSON data to a "class" (.Net object).
Now let's see how to loop through JSON array using Vb.Net.
Assuming, this is my JSON array. Its an array of "birds" with an unique ID, the name of the bird and type of bird. Three distinct types.
Dim arr As String = "[{ ""ID"": ""001"", ""Name"": ""Eurasian Collared-Dove"", ""Type"": ""Dove"" }, " & _ "{ ""ID"": ""002"", ""Name"": ""Bald Eagle"", ""Type"": ""Hawk"" }, " & _ "{ ""ID"": ""003"", ""Name"": ""Cooper's Hawk"", ""Type"": ""Hawk"" }]"
Code (VB.Net)
Option Explicit On Imports System.Web.Script.Serialization ' For JSON deserialization. Partial Class _Default Inherits System.Web.UI.Page ' The JSON array Dim arr As String = "[{ ""ID"": ""001"", ""Name"": ""Eurasian Collared-Dove"", ""Type"": ""Dove"" }, " & _ "{ ""ID"": ""002"", ""Name"": ""Bald Eagle"", ""Type"": ""Hawk"" }, " & _ "{ ""ID"": ""003"", ""Name"": ""Cooper's Hawk"", ""Type"": ""Hawk"" }]" ' Defind a class with 3 properties. Public Class birds Public ID As String Public Name As String Public Type As String End Class Dim birdsList As New List(Of birds)() ' Read JSON array and loop through each object and display the values. Private Sub read_json() Dim json As JavaScriptSerializer = New JavaScriptSerializer() birdsList = json.Deserialize(Of List(Of birds))(arr) ' Loop through each object and display the values. For Each val As birds In birdsList If result.InnerHtml = "" Then result.InnerHtml = val.ID + " " + val.Name + " " + val.Type Else result.InnerHtml = result.InnerHtml + "<br />" + val.ID + " " + val.Name + " " + val.Type End If Next End Sub Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load If Not IsPostBack Then read_json() End If End Sub End Class
First, import System.Web.Script.Serialization namespace. So, we can access JavaScriptSerializer class and its properties.
Next, I have defined a class named "birds", so I can convert JSON data to an object. The JSON array is then "Deserialized" and stored in an object of birds class.
birdsList = json.Deserialize(Of List(Of birds))(arr)
Finally, I am calling a procedure named "read_json()" when the form loads. It will deserialized the array and then displays the values on the web page.
Read "external JSON file" and loop through each object using Vb.Net
In the above example, I have declared an array and assigned JSON value in it.
Now, using a similar method, we'll first read an external JSON file, deserialized the JSON data, loop through each object and its values using Vb.Net.
Assuming, I have a JSON file named birds.json in the root directory.
Here's the VB code.
Option Explicit On Imports System.IO ' For StreamReader() Imports System.Web.Script.Serialization ' For JSON de-serialization. Partial Class _Default Inherits System.Web.UI.Page Dim arr As String = Server.MapPath("birds.json") ' Defind a class with 3 properties. Public Class birds Public ID As String Public Name As String Public Type As String End Class Dim birdsList As New List(Of birds)() Private Sub read_json() Using sr As New StreamReader(arr) Dim json As JavaScriptSerializer = New JavaScriptSerializer() birdsList = json.Deserialize(Of List(Of birds))(sr.ReadToEnd()) For Each val As birds In birdsList If result.InnerHtml = "" Then result.InnerHtml = val.ID + " " + val.Name + " " + val.Type Else result.InnerHtml = result.InnerHtml + "<br />" + val.ID + " " + val.Name + " " + val.Type End If Next End Using End Sub Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load If Not IsPostBack Then read_json() End If End Sub End Class
There are "two" namespaces in the beginning.
Imports System.IO
Imports System.Web.Script.Serialization
1) The System.OI namespace contains the file and directory classes. It provides methods and properties to manipulate, extract or read files and directories.
2) The System.Web.Script.Serialization namespace is used to "serialize" and "deserialize" JSON object.
The remaining code is the same as the first example above. There is one difference. This time it will Deserialize JSON "extracted from the file". In the previous example, if you have noticed, it was the "array".
birdsList = json.Deserialize(Of List(Of birds))(sr.ReadToEnd())