如何解析json文件中的值并在vb.net的消息框中返回值?

时间:2019-05-10 07:52:30

标签: json vb.net

我正在使用vb.net开发Windows窗体应用程序,其中用户在文本框中输入ID,然后在MessageBox中返回值。

我现在遇到的问题是如何解析文本框中的值,然后如何将其与json文件本身中的现有值进行比较。

这是Button类的代码。

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        myFiles = MyFileObjects.Deserialize(File.ReadAllText("[FilePath]"))
        Dim json = MyFileObjects.Serialize(myFiles)
        File.WriteAllText("[FilePath]", json)

    End Sub

这是公共类的模块

 Public Class MyFileObjects
        Public Class MyFile
            <JsonProperty("filename")>
            Public Property FileName As String
            <JsonProperty("title")>
            Public Property Title As String
            <JsonProperty("type")>
            Public Property Type As String
        End Class

        Public Shared Function Serialize(myFiles As List(Of MyFile)) As String
            Return JsonConvert.SerializeObject(myFiles)
        End Function
        Public Shared Function Deserialize(json As String) As List(Of MyFile)
            Return JsonConvert.DeserializeObject(Of List(Of MyFile))(json)
        End Function
    End Class

这是针对文本框的。

Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyValue = Keys.Enter Then
            Dim selectedItem = myFiles.Find(Function(f) f.FileName.Equals(TextBox1.Text))
            If selectedItem IsNot Nothing Then
                MessageBox.Show(selectedItem.Title)
            End If
        End If
    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        TextBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
        TextBox1.AutoCompleteCustomSource.AddRange(myFiles.Select(Function(f) f.FileName).ToArray())
    End Sub

这是我的json文件的内容

[
  {
    "filename": "123.xml",
    "title": "Hello",
    "type": "PU"
  },
  {
    "filename": "456.xml",
    "title": "World",
    "type": "PU"
  },
  {
    "filename": "258.xml",
    "title": "Test",
    "type": "PU"
  }
]

希望用户在文本框字段中搜索“数据文件名”,并在消息框中显示“标题”。现在,我只能继续下一步。你们有什么建议吗?我需要指导,因为我是vb.net的新手。非常感谢

1 个答案:

答案 0 :(得分:1)

您的JSON是简单对象的列表/数组。可以使用单个Class对象将其反序列化/序列化:
(名为 myFiles 的私有字段用于包含反序列化的对象)

Private myFiles As List(Of MyFileObjects.MyFile)

Public Class MyFileObjects
    Public Class MyFile
        <JsonProperty("filename")>
        Public Property FileName As String
        <JsonProperty("Title")>
        Public Property Title As String
        <JsonProperty("type")>
        Public Property Type As String
    End Class

    Public Shared Function Serialize(myFiles As List(Of MyFile)) As String
        Return JsonConvert.SerializeObject(myFiles)
    End Function
    Public Shared Function Deserialize(json As String) As List(Of MyFile)
        Return JsonConvert.DeserializeObject(Of List(Of MyFile))(json)
    End Function
End Class

请注意,我添加了:

  • 一个<JsonProperty()>属性:当列表/数组被序列化时,它可以修改类属性的名称,同时保留在原始JSON中找到的名称。当JSON包含该语言的保留关键字(例如operatortype等)时,这非常有用
  • 允许通过一次调用对JSON进行反序列化/序列化的两个方法。

JSON反序列化为 List(Of MyFileObjects.MyFile ):

myFiles = MyFileObjects.Deserialize(File.ReadAllText("[File Path]"))

List(Of MyFileObjects.MyFile 序列化为JSON

Dim json = MyFileObjects.Serialize(myFiles)

创建文本框的AutoCompleteCustomSource以启用其自动完成功能:
AutoCompleteModeAutoCompleteSource属性可以在表单设计器中预先设置)

TextBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
TextBox1.AutoCompleteCustomSource.AddRange(myFiles.Select(Function(f) f.FileName).ToArray())

当用户使用“自动完成”功能选择项目时显示消息框:

请注意,当从“自动完成”列表中选择某项时,“自动完成”将向所选文本添加 Key.Enter 。您不需要按 Enter 键。

Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
    If e.KeyValue = Keys.Enter Then
        Dim selectedItem = myFiles.Find(Function(f) f.FileName.Equals(TextBox1.Text))
        If selectedItem IsNot Nothing Then
            MessageBox.Show(selectedItem.Title)
        End If
    End If
End Sub

将一个项目添加到对象列表,然后序列化并保存JSON

myFiles.Add(New MyFileObjects.MyFile() With {
    .FileName = "500.xml",
    .Title = "Title 500",
    .Type = "PU"
})

Dim json = MyFileObjects.Serialize(myFiles)
File.WriteAllText("[File Path]", json)

完整代码:

Public Class Form1

    Private myFiles As List(Of MyFileObjects.MyFile)
    Private jsonPath As String = String.Empty

    Public Class MyFileObjects
        Public Class MyFile
            <JsonProperty("filename")>
            Public Property FileName As String
            <JsonProperty("Title")>
            Public Property Title As String
            <JsonProperty("type")>
            Public Property Type As String
        End Class

        Public Shared Function Serialize(myFiles As List(Of MyFile)) As String
            Return JsonConvert.SerializeObject(myFiles, Formatting.Indented)
        End Function

        Public Shared Function Deserialize(json As String) As List(Of MyFile)
            Return JsonConvert.DeserializeObject(Of List(Of MyFile))(json)
        End Function
    End Class

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        jsonPath = "[Insert your JSON Path here]"
        myFiles = LoadJSON(jsonPath)
        TextBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
        TextBox1.AutoCompleteCustomSource.AddRange(myFiles.Select(Function(f) f.FileName).ToArray())
    End Sub

    Private Function LoadJSON(JSONPath As String) As List(Of MyFileObjects.MyFile)
        Return MyFileObjects.Deserialize(File.ReadAllText(JSONPath))
    End Function

    Private Sub SaveJSON(filePath As String, objects As List(Of MyFileObjects.MyFile))
        File.WriteAllText(filePath, MyFileObjects.Serialize(objects))
    End Sub

    Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyValue = Keys.Enter Then
            Dim selectedItem = myFiles.Find(Function(f) f.FileName.Equals(TextBox1.Text))
            If selectedItem IsNot Nothing Then
                MessageBox.Show(selectedItem.Title)
            End If
        End If
    End Sub
End Class

Private Sub buttonSaveJson_Click(sender As Object, e As EventArgs) Handles buttonSaveJson.Click
    SaveJSON(jsonPath, myFiles)
End Sub