How to Access and Parse a Value of JSON in VB.NET - Catatan Si Boy
post-template-default,single,single-post,postid-515,single-format-standard,eltd-core-1.1.1,flow child-child-ver-1.0.0,flow-ver-1.3.7,,eltd-smooth-page-transitions,ajax,eltd-grid-1300,eltd-blog-installed,page-template-blog-standard,eltd-header-vertical,eltd-sticky-header-on-scroll-up,eltd-default-mobile-header,eltd-sticky-up-mobile-header,eltd-dropdown-default

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

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

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

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


No Comments

Post a Comment