How To Parse & Read JSON In VB.NET
JSON , Known As JavaScript Object Notation , Is An Important Format Which We Mostly Get As Output When Requesting Data By API. In This Post We Will See How To Read , Parse JSON Using Visual Basic .NET (VB.NET)
VB.NET Parse JSON
- Open Visual Studio
- Create A New Windows Form App Project (Visual Basic)
- Add One TextBox (txt1) & One Button (Button1) In The Project
- TextBox Will Be Used To Print The Parsed JSON & Button Will Be Used To Parse The JSON
- Go To Project->Add Project Reference->Browse And Add Newtonsoft.Json.dll [ Download | Mirror ]
Project Structure
VB.NET Parse JSON Form Application |
VB.NET Code To Parse JSON
Firstly , We Need To Import System.Net & Newtonsoft.Json.Linq . After That We Need The URL Of Target JSON. In This Post , I Will Use http://time.jsontest.com/ As The JSON URL Which Provide Current Date & Time.
Read JSON Using VB.NET
Imports System.Net
Imports Newtonsoft.Json.Linq
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
Dim json As String = New System.Net.WebClient().DownloadString("http://time.jsontest.com/")
Dim parsejson As JObject = JObject.Parse(json)
Dim thedate = parsejson.SelectToken("date").ToString()
txt1.Text = "Date Is "+thedate
End Sub
End Class
Output Of VB.NET Parsing JSON
VB.NET JSON Application |
In The Above Code , We Have Printed Date From The JSON URL
VB.NET Parse Sub Node Of JSON
Lets Try Another JSON URL Which Consist Node & Sub Nodes. I Am Taking An URL , i.e. https://reqres.in/api/products/3
Imports System.Net
Imports Newtonsoft.Json.Linq
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
Dim json As String = New System.Net.WebClient().DownloadString("https://reqres.in/api/products/3")
Dim parsejson As JObject = JObject.Parse(json)
Dim thename = parsejson.SelectToken("data.name").ToString()
txt1.Text = "Name Is " + thename
End Sub
End Class
5 comments