在Visual Basic中反序列化JSON [英] Deserializing JSON in Visual basic

查看:51
本文介绍了在Visual Basic中反序列化JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

基本上,有一个富文本框称为input,另一个称为post_text_box.我想做的是使它来自输入文本框中输入的4chan线程的JSON,并从该JSON中提取注释并显示在输出文本框中

basically, there is one rich text box called input, and another called post_text_box. What im trying to do is make it so that JSON from a 4chan thread entered in the input text box, and comments are extracted from that JSON and displayed in the output text box

但是,每当我尝试单击执行"按钮时,都不会发生任何事情.

however, whenever I try clicking 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

推荐答案

由于要导入Newtonsoft.Json,因此您可以仅使用

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

或者,如果您不想为Post创建类,则可以使用

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

编辑:您似乎正在从API获取数组:

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
        }
    ]
}

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

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

这篇关于在Visual Basic中反序列化JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆