栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Visual Basic中反序列化JSON

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Visual Basic中反序列化JSON

由于您正在导入

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/437110.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号