vb.net使用Newtonsoft.Json读取Json文件

环境: VS2017社区版
目的: 读取json的key作为combobox的选项,并把对应的value写入textbox
json文件命名为Dm.json, 存放在debug路径

{
	"A":1,
	"B":2,
	"C":3
}

1. 下载Newtonsoft.Json. (官方地址:https://www.newtonsoft.com/json)
可以直接通过VS 2017进行下载并引用
1.1 右击引用,选择"管理NuGet程序包"
vb.net使用Newtonsoft.Json读取Json文件
1.2 在浏览里面搜索Json,选择下载即可
2. Dm类,用以读取以及维护json文件

Imports System.IO
'导入Newtonsoft.Json
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

Public Class Dm

    Private dm_dict As Hashtable = New Hashtable()

    '构造函数
    Public Sub New()
        ReadJson()
    End Sub

    '属性dm_dict的getter与setter
    Public Property Dm_dict1 As Hashtable
        Get
            Return dm_dict
        End Get
        Set(value As Hashtable)
            dm_dict = value
        End Set
    End Property
    '读取Json文件
    Public Sub ReadJson()
        'exe路径下的json文件
        Dim FilePath = Environment.CurrentDirectory() & "\dm.json"
        Dim strJson = ""
        Dim obj As JObject = New JObject() 'JObject用来接收读取并转换后的数据
        Try
            '读取文件
            Using sr As StreamReader = New StreamReader(FilePath)
                Dim Line = sr.ReadLine()
                strJson = Line
                While (Line <> Nothing)
                    Line = sr.ReadLine()
                    strJson = strJson & Line
                    Console.WriteLine(Line)
                End While
                'Console.Write("=============")
                'Console.WriteLine(str)
            End Using
            obj = JsonConvert.DeserializeObject(strJson) '转为JObject
            '遍历并存入Hashtable dm_dict
            For Each item In obj
                Dm_dict1.Add(item.Key, item.Value)
            Next
        Catch e As Exception
            Throw e
        End Try
    End Sub

    '获取所有的Key
    Public Function GetKeys()
        Return Dm_dict1.Keys()
    End Function

    '通过key获取对应的值
    Public Function GetValue(key As String)
        If key Is Nothing Then
            Return 0
        Else
            Return Dm_dict1(key)
        End If
    End Function
End Class

3. Module1

Module Module1
    Public dm As Dm = New Dm()
    '获取数据,并放入comboBox
    Public Sub LoadDmUnit()
        Dim keys As ICollection = dm.GetKeys()
        For Each key In keys
            Form1.cbxDmUnit.Items.Add(key)
        Next
    End Sub

    '根据combobox选项的变化,将对应值填入textbox
    Public Sub LoadDmValue()
        Form1.tbDmValue.Text = dm.GetValue(Form1.cbxDmUnit.SelectedItem)
    End Sub
End Module

4. Form1

Public Class Form1
    '加载Sub, 用来初始化
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadDmUnit()
    End Sub
    'combobox选项变更事件,当选项变化,变化对应的值
    Private Sub cbxDmUnit_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbxDmUnit.SelectedIndexChanged
        LoadDmValue()
    End Sub
End Class

5. 界面
vb.net使用Newtonsoft.Json读取Json文件
6. 演示
vb.net使用Newtonsoft.Json读取Json文件

备注:没有系统学过VB.NET, 只是工作中有用到,用来开发一些小工具. 不足之处,还望海涵, 谢谢

上一篇:5大UX设计谬论,如何去补救?


下一篇:ASP.NET Razor – VB 逻辑条件简介