[VB.NET] Parsing JSON using Newtonsoft Library


Deskripsi
       To get some value in json format for VB.NET

Howto
1. Download Newtonsoft library here
2. Add reference at Project > Add Reference and Imports Newtonsoft.Json in your code
3. Copy paste code below :
   'create sample json string
   Dim jsontext As String = "{""name"":""mahendra"",""language"":""vb.net"",""settings"":{""framework"":""3.5""}}"
 
   'read the jsontext
    Dim readingJson = Newtonsoft.Json.Linq.JObject.Parse(jsontext)
    MessageBox.Show(readingJson.Item("settings")("framework").ToString)

   'it will return value : "3.5"
   it's not working if the type json array (json which have some bracket like this '[' or ']') 

But , if you want deserialize JSON without using this library , you can use .NET library (.NET 4.0 FULL not CLIENT PROFILE)

#Start Here
Imports System.Web.Script.Serialization

Dim js As New System.Web.Script.Serialization.JavaScriptSerializer
Dim data = js.Deserialize(Of Dictionary(Of Object, Object))(json)
Dim dataDeserial = data.Values

msgbox("Count : " & dataDeserial(X)(X)(X........).Length)

For i = 0 To dataDeserial(X)(X)(X........).Length - 1
       ' do something
Next i

'Note : (X)(X)(X........) ==> based on how deep your JSON

2 comments

Thank you! Just what I needed to get json.net working under vb.net

Reply

Post a Comment

Harap gunakan bahasa yang baik dan sopan, terima kasih