gpt4 book ai didi

json - 在 Visual Basic 中反序列化 JSON

转载 作者:行者123 更新时间:2023-12-04 09:05:18 24 4
gpt4 key购买 nike

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

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

但是,每当我尝试单击 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

最佳答案

由于您正在导入 Newtonsoft.Json ,您只需使用 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 创建一个类,您可以使用 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 获取一个数组:
{
"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() :

首先,添加另一个小包装类:
Public Class PostWrapper
Public posts() As Post
End Class

然后调整你的反序列化代码:
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

关于json - 在 Visual Basic 中反序列化 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20079177/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com