在Visual Basic中反序列化JSON

use*_*090 7 vb.net json

基本上,我正在尝试使用4chan JSON API解析来自4chan线程的注释.https://github.com/4chan/4chan-API

基本上,有一个名为input的富文本框,另一个名为post_text_box.什么即时试图做的是让这个JSON从输入文本框中输入和评论是从JSON提取并显示在输出文本框中4chan的线程

但是,每当我尝试点击Go按钮时都没有任何反应.

到目前为止,这是我的代码

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
Run Code Online (Sandbox Code Playgroud)

val*_*rij 19

由于您正在导入Newtonsoft.Json,您可以使用以下JsonConvert.DeserializeObject<T>(String)方法:

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
Run Code Online (Sandbox Code Playgroud)

或者,如果您不想为其创建类Post,则可以使用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
Run Code Online (Sandbox Code Playgroud)

编辑:看起来你从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
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您需要将要反序列化的类型更改为Post():

首先,添加另一个小包装类:

Public Class PostWrapper
    Public posts() As Post
End Class
Run Code Online (Sandbox Code Playgroud)

然后调整反序列化代码:

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
Run Code Online (Sandbox Code Playgroud)


Kar*_*son 8

您可以将JSON反序列化为一个类,而不是需要定义一个类Object,如下所示:

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)
Run Code Online (Sandbox Code Playgroud)

现在,作为示例,您可以遍历反序列化的JSON并构建HTML表,如下所示:

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()
Run Code Online (Sandbox Code Playgroud)

免责声明:我每天都使用C#,这是一个C#示例使用dynamic转换为VB.NET,如果有任何语法错误,请原谅我.