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

WinForms VB.NET中用Newtonsoft.Json填充lbxCamera列表框求助

VB.NET WinForms 使用Newtonsoft.Json填充ListBox步骤

1. 安装Newtonsoft.Json包

  • 打开Visual Studio,右键你的WinForms项目 → 「管理NuGet程序包」
  • 在浏览标签搜索Newtonsoft.Json,选择最新稳定版安装

2. 定义JSON对应的实体类

根据你提供的JSON结构,创建两个类用于反序列化:

Public Class CameraInstructionsContainer
    Public Property lbxCamera As List(Of InstructionItem)
End Class

Public Class InstructionItem
    Public Property Instructions As String
End Class

3. 读取JSON并填充ListBox

在窗体的加载事件(或其他触发逻辑)中添加以下代码:

Imports Newtonsoft.Json
Imports System.IO

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Try
        ' 替换为你的JSON文件实际路径
        Dim jsonFilePath As String = "C:\example\your_json_file.json"
        ' 读取JSON文件内容
        Dim jsonContent As String = File.ReadAllText(jsonFilePath)
        
        ' 反序列化JSON到实体类
        Dim instructionsContainer As CameraInstructionsContainer = JsonConvert.DeserializeObject(Of CameraInstructionsContainer)(jsonContent)
        
        ' 清空ListBox并添加所有Instructions项
        lbxCamera.Items.Clear()
        For Each item In instructionsContainer.lbxCamera
            lbxCamera.Items.Add(item.Instructions)
        Next
    Catch ex As Exception
        MessageBox.Show($"操作失败:{ex.Message}")
    End Try
End Sub

如果你的JSON是直接存储在字符串变量中(而非文件),可以跳过文件读取步骤,直接使用字符串:

Dim jsonContent As String = "{""lbxCamera"":[{""Instructions"":""2nd Instruction""},{""Instructions"":""2nd Instruction""},{""Instructions"":""3rd Instruction""}]}"
Dim instructionsContainer As CameraInstructionsContainer = JsonConvert.DeserializeObject(Of CameraInstructionsContainer)(jsonContent)
' 后续填充ListBox代码同上

内容的提问来源于stack exchange,提问作者Tarek Basta

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 18:52:32