由于您正在导入
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.compost_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.compost_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 PostEnd Class
然后调整反序列化代码:
Dim json As String = input_box.TextDim postWrapper = JsonConvert.DeserializeObject(Of PostWrapper)(json) ' Deserialize array of Post objectsDim posts = postWrapper.postsIf posts.Length = 1 Then ' or whatever condition you prefer post_text_box.Text = posts(0).comEnd If



