You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Visual Basic UWP项目中如何将JSON字符串转换为JSON数组并输出

实现步骤

1. 安装JSON序列化依赖

推荐使用Newtonsoft.Json(Json.NET)处理JSON,操作更便捷:右键你的UWP项目 -> 选择「管理NuGet包」 -> 搜索Newtonsoft.Json完成安装即可。
如果你不想使用第三方库,也可以用.NET内置的System.Text.Json,用法逻辑一致。

2. 定义匹配JSON结构的实体类

新建一个类和JSON字段一一对应,代码如下:

Public Class UserInfo
    Public Property id As Integer
    Public Property name As String
    Public Property lname As String
    Public Property phone As Long
End Class

3. 反序列化JSON并输出到文本框

在你已经拿到Data字符串的代码后面,追加如下逻辑即可:

' 顶部先引入命名空间
Imports Newtonsoft.Json
Imports System.Text

' 反序列化JSON字符串为用户对象列表
Dim userList As List(Of UserInfo) = JsonConvert.DeserializeObject(Of List(Of UserInfo))(Data)

' 拼接所有用户的信息
Dim outputStr As New StringBuilder()
For Each user In userList
    outputStr.AppendLine($"用户ID:{user.id}")
    outputStr.AppendLine($"名字:{user.name}")
    outputStr.AppendLine($"姓氏:{user.lname}")
    outputStr.AppendLine($"电话:{user.phone}")
    outputStr.AppendLine("------------------------")
Next

' 赋值到文本框,注意UWP中跨线程更新UI需要调度
If Me.Dispatcher.HasThreadAccess Then
    ' 把txtOutput替换为你实际的文本框控件名称
    txtOutput.Text = outputStr.ToString()
Else
    Await Me.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, Sub()
        txtOutput.Text = outputStr.ToString()
    End Sub)
End If

补充说明

如果使用内置的System.Text.Json,只需要把反序列化的代码替换为下面的内容即可,其余逻辑不变:

Imports System.Text.Json

Dim userList As List(Of UserInfo) = JsonSerializer.Deserialize(Of List(Of UserInfo))(Data)

内容的提问来源于stack exchange,提问作者Pratik GT

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 09:06:03