banner
Jan 14, 2020
10 Views

How to Access and Parse a Value of JSON in VB.NET

Written by
banner

The json looks like:

[
  {Id:1, view,:true , write:false},
  {Id:2, view: true , write:true}, ..etc
 ]

 

Imported Libraries:

Imports System.Web.Script.Serialization
Imports System.IO

The Part of the code that will get the json from the request:

Dim tree_data As String = Nothing
    HttpContext.Current.Request.InputStream.Position = 0
    Using inputStream As StreamReader = New StreamReader(HttpContext.Current.Request.InputStream)
        tree_data = inputStream.ReadToEnd()
    End Using

Dim jsonSerializer As System.Web.Script.Serialization.JavaScriptSerializer = 
New System.Web.Script.Serialization.JavaScriptSerializer()

Dim permissions = jsonSerializer.Deserialize(Of List(Of Object))(tree_data)

The Part of the code that parsing the json to extract values, and may be this piece of code can be written in a different way but that’s what I used:

For Each obj In permissions 
            Dim id As Integer
            Dim view As Boolean
            Dim write As Boolean
             For Each pair In obj // each object is a pair of key and value
                If pair.Key.Equals("Id") Then
                   id = pair.Value
                ElseIf pair.Key.Equals("view") Then
                   view = pair.Value
                ElseIf pair.Key.Equals("write") Then
                   write = pair.Value
                End If

             Next
           // Do something with the current id , view ,write parameters
     Next

 

Article Tags:
·
Article Categories:
JSON · VB.NET
banner
    https://mfaisal.com

    Seseorang yang selalu tertarik dengan Computer Programming, Photoshop, Web Design, Motivasi, Wordpress dan Internet. Silahkang lingkari saya di Google+ : Faisal dan Facebook. Atau follow juga di Twitter

    Leave a Reply

    Your email address will not be published. Required fields are marked *