Removing JSON Deserialization in Visual Basic

Basically, I am trying to parse comments from a 4chan stream using the 4chan JSON API. https://github.com/4chan/4chan-API

basically, there is one rich text field named input, and the other is post_text_box. What I'm trying to do is make the JSON from the 4chan stream injected into the input text box and the comments are pulled from that JSON and displayed in the output text box

however, whenever I try to press the Go button, nothing happens.

Here is my code so far

Imports System.Web.Script.Serialization Imports Newtonsoft.Json Imports Newtonsoft.Json.Linq Public Class Form1 Private Sub start_button_Click(sender As Object, e As EventArgs) Handles start_button.Click Dim j As Object = New JavaScriptSerializer().Deserialize(Of Post)(input.Text) post_text_box.Text = j.com End Sub End Class Public Class Rootobject Public Property posts() As Post End Class Public Class Post Public Property no As Integer Public Property now As String Public Property name As String Public Property com As String Public Property filename As String Public Property ext As String Public Property w As Integer Public Property h As Integer Public Property tn_w As Integer Public Property tn_h As Integer Public Property tim As Long Public Property time As Integer Public Property md5 As String Public Property fsize As Integer Public Property resto As Integer Public Property bumplimit As Integer Public Property imagelimit As Integer Public Property replies As Integer Public Property images As Integer End Class 
+7
json
Nov 19 '13 at 18:25
source share
5 answers

Since you are importing Newtonsoft.Json , you can simply use the JsonConvert.DeserializeObject<T>(String) method :

 Dim exampleJson As String = "{ 'no':'123', 'name':'Some Name', 'com':'This is a comment'}" Dim post As Post = JsonConvert.DeserializeObject(Of Post)(exampleJson) Dim com As String = post.com post_text_box.Text = com 

Alternatively, if you do not want to create a class for Post , you can use JsonConvert.DeserializeAnonymousType<T>(String, T) :

 Dim exampleJson As String = "{ 'no':'123', 'name':'Some Name', 'com':'This is a comment'}" Dim tempPost = New With {Key .com = ""} Dim post = JsonConvert.DeserializeAnonymousType(exampleJson, tempPost) Dim com As String = post.com post_text_box.Text = com 

EDIT : It looks like you are getting an array from the API:

 { "posts" : [{ "no" : 38161812, "now" : "11\/19\/13(Tue)15:18", "name" : "Anonymous", "com" : β€Œβ€‹ "testing thread for JSON stuff", "filename" : "a4c", "ext" : ".png", "w" : 386, "h" : 378, "tn_w" : 250, "tn_h" : 244, "tim" β€Œβ€‹ : 1384892303386, "time" : 1384892303, "md5" : "tig\/aNmBqB+zOZY5upx1Fw==", "fsize" : 6234, "β€Œβ€‹resto" : 0, "bumplimit" : 0, "imagelimit" : 0, "replies" : 0, "images" : 0 } ] } 

In this case, you will need to change the type of the deserialized type to Post() :

First add another small wrapper class:

 Public Class PostWrapper Public posts() As Post End Class 

Then configure the deserialization code:

 Dim json As String = input_box.Text Dim postWrapper = JsonConvert.DeserializeObject(Of PostWrapper)(json) ' Deserialize array of Post objects Dim posts = postWrapper.posts If posts.Length = 1 Then ' or whatever condition you prefer post_text_box.Text = posts(0).com End If 
+19
Nov 19 '13 at 19:42 on
source share

Instead of defining a class, you can deserialize JSON to Object , for example:

 Dim json As String = "{""items"":[{""Name"":""John"",""Age"":""20"",""Gender"":""Male""},{""Name"":""Tom"",""Age"":""25"",""Gender"":""Male""},{""Name"":""Sally"",""Age"":""30"",""Gender"":""Female""}]}" Dim jss = New JavaScriptSerializer() Dim data = jss.Deserialize(Of Object)(json) 

Now, as an example, you can scroll through the deserialized JSON and create an HTML table, for example:

 Dim sb As New StringBuilder() sb.Append("<table>" & vbLf & "<thead>" & vbLf & "<tr>" & vbLf) ' Build the header based on the keys of the first data item. For Each key As String In data("items")(0).Keys sb.AppendFormat("<th>{0}</th>" & vbLf, key) Next sb.Append("</tr>" & vbLf & "</thead>" & vbLf & "<tbody>" & vbLf) For Each item As Dictionary(Of String, Object) In data("items") sb.Append("<tr>" & vbLf) For Each val As String In item.Values sb.AppendFormat(" <td>{0}</td>" & vbLf, val) Next Next sb.Append("</tr>" & vbLf & "</tbody>" & vbLf & "</table>") Dim myTable As String = sb.ToString() 

Disclaimer: I work daily with C #, and this is an example of C # using dynamic , which was converted to VB.NET, please forgive me if there are any syntax errors with this.

+8
Nov 19 '13 at 19:36
source share

Also, if you have a complex json string. If the json string has subclasses, arrays, etc., you can use this method below. I tried and it worked for me. Hope this will be helpful for you.

In the json line, I used root-> simpleforecast-> forecastday [] β†’ date-> hight-> celsius, fahrenheit, etc.

 Dim tempforecast = New With {Key .forecast = New Object} Dim sFile As String = SimpleTools.RWFile.ReadFile("c:\\testjson\\test.json") Dim root = JsonConvert.DeserializeAnonymousType(sFile, tempforecast) Dim tempsimpleforecast = New With {Key .simpleforecast = New Object} Dim forecast = jsonConvert.DeserializeAnonymousType(root.forecast.ToString(), tempsimpleforecast) Dim templstforecastday = New With {Key .forecastday = New Object} Dim simpleforecast = JsonConvert.DeserializeAnonymousType(forecast.simpleforecast.ToString(), templstforecastday) Dim lstforecastday = simpleforecast.forecastday For Each jforecastday In lstforecastday Dim tempDate = New With {Key .date = New Object, .high = New Object, .low = New Object} Dim forecastday = JsonConvert.DeserializeAnonymousType(jforecastday.ToString(), tempDate) Dim tempDateDetail = New With {Key .day = "", .month = "", .year = ""} Dim fcDateDetail = JsonConvert.DeserializeAnonymousType(forecastday.date.ToString(), tempDateDetail) Weather_Forcast.ForcastDate = fcDateDetail.day.ToString() + "/" + fcDateDetail.month.ToString() + "/" + fcDateDetail.year.ToString() Dim temphighDetail = New With {Key .celsius = "", .fahrenheit = ""} Dim highDetail = JsonConvert.DeserializeAnonymousType(forecastday.high.ToString(), temphighDetail) Dim templowDetail = New With {Key .celsius = "", .fahrenheit = ""} Dim lowDetail = JsonConvert.DeserializeAnonymousType(forecastday.low.ToString(), templowDetail) Weather_Forcast.highCelsius = Decimal.Parse(highDetail.celsius.ToString()) Weather_Forcast.lowCelsius = Decimal.Parse(lowDetail.celsius.ToString()) Weather_Forcast.highFahrenheit = Decimal.Parse(lowDetail.fahrenheit.ToString()) Weather_Forcast.lowFahrenheit = Decimal.Parse(lowDetail.fahrenheit.ToString()) Weather_Forcast09_Result.Add(Weather_Forcast) Next 
0
Dec 16 '15 at 9:42
source share

Note:

First you need to install Newtonsoft.Json on the nuget console. Then add the following code on top of your code.

  Imports Newtonsoft.Json 

Step: 1 Create a class with get and set settings.

 Public Class Student Public Property rno() As String Get Return m_rno End Get Set(value As String) m_rno = value End Set End Property Private m_rno As String Public Property name() As String Get Return m_name End Get Set(value As String) m_name = value End Set End Property Private m_name As String Public Property stdsec() As String Get Return m_StdSec End Get Set(value As String) m_StdSec = value End Set End Property Private m_stdsec As String End Class 

Step: 2 Create a string as a json format and change it as a json object model.

  Dim json As String = "{'rno':'09MCA08','name':'Kannadasan Karuppaiah','stdsec':'MCA'}" Dim stuObj As Student = JsonConvert.DeserializeObject(Of Student)(json) 

Step: 3 Just navigate by the name of object.entity as follows.

 MsgBox(stuObj.rno) MsgBox(stuObj.name) MsgBox(stuObj.stdsec) 
0
02 Oct. '17 at 9:51 on
source share

prog2011, can you show your JSON file so that I can evaluate how your code works?

it's still hard for me to parse the nested values.

0
Jul 21 '19 at 9:45
source share



All Articles