Deserializing JSON in Visual basic

Solution 1:

Since you're importing Newtonsoft.Json, you can just 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 don't 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're getting an array back 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 that case, you will need to change the type that is being deserialized to Post():

First, add another small wrapper class:

Public Class PostWrapper
    Public posts() As Post
End Class

Then adjust your 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

Solution 2:

Instead of needing to define a class, you can deserialize the JSON into an Object, like this:

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 could loop through the deserialized JSON and build an HTML table, like this:

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 with C# on a daily basis and this is a C# example using dynamic that was converted to VB.NET, please forgive me if there are any syntax errors with this.