如何在vb.net中解析json文件中的值并返回messagebox中的值?
我正在使用vb.net开发一个Windows窗体应用程序,其中用户在文本框中输入一个ID,然后在消息框中返回一个值 我现在面临的问题是如何解析文本框中的值,然后如何将其与json文件本身中的现有值进行比较 这是Button类的代码如何在vb.net中解析json文件中的值并返回messagebox中的值?,json,vb.net,Json,Vb.net,我正在使用vb.net开发一个Windows窗体应用程序,其中用户在文本框中输入一个ID,然后在消息框中返回一个值 我现在面临的问题是如何解析文本框中的值,然后如何将其与json文件本身中的现有值进行比较 这是Button类的代码 Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click myFiles = MyFileObjects.Deserialize(File.Re
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
这是我的json文件的内容
[
{
"filename": "123.xml",
"title": "Hello",
"type": "PU"
},
{
"filename": "456.xml",
"title": "World",
"type": "PU"
},
{
"filename": "258.xml",
"title": "Test",
"type": "PU"
}
]
用户需要在textbox字段中搜索“datafilename”,并在messagebox中显示“title”。现在我陷入了下一步该怎么办的困境。你们有什么建议吗?我需要指导,因为我是vb.net新手。非常感谢您的
JSON
是一个简单对象的列表/数组。可以使用单个类对象反序列化/序列化它:(名为myFiles的私有字段用于包含反序列化对象) 将(MyFileObjects.MyFile的)列表序列化回一个
JSON
:
Dim json = MyFileObjects.Serialize(myFiles)
myFiles.Add(New MyFileObjects.MyFile() With {
.FileName = "500.xml",
.Title = "Title 500",
.Type = "PU"
})
Dim json = MyFileObjects.Serialize(myFiles)
File.WriteAllText("[File Path]", json)
创建文本框的名称以启用其自动完成功能:(可以在窗体的设计器中预先设置和属性) 当用户使用自动完成功能选择项目时显示消息框: 请注意,“自动完成”将添加一个
键。当从“自动完成”列表中选择项目时,请在所选文本中输入
。您不需要按Enter键
向对象列表中添加一项,然后序列化并保存JSON
:
Dim json = MyFileObjects.Serialize(myFiles)
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
公共类表单1
私有myFiles作为列表(MyFileObjects.MyFile)
私有jsonPath为String=String.Empty
公共类MyFileObjects
公共类MyFile
公共属性文件名为字符串
公共财产所有权作为字符串
公共属性类型为字符串
末级
公共共享函数将(myFiles作为列表(MyFile的))序列化为字符串
返回JsonConvert.Serialized对象(myFiles,Formatting.Indented)
端函数
公共共享函数反序列化(json为字符串)为(MyFile的)列表
返回JsonConvert.DeserializeObject(属于列表(属于MyFile))(json)
端函数
末级
私有子表单1_Load(发送方作为对象,e作为事件参数)处理MyBase.Load
jsonPath=“[在此处插入JSON路径]”
myFiles=LoadJSON(jsonPath)
TextBox1.AutoCompleteMode=AutoCompleteMode.SuggestAppend
TextBox1.AutoCompleteSource=AutoCompleteSource.CustomSource
TextBox1.AutoCompleteCustomSource.AddRange(myFiles.Select(Function(f)f.FileName.ToArray())
端接头
私有函数LoadJSON(JSONPath作为字符串)作为列表(MyFileObjects.MyFile)
返回MyFileObjects.Deserialize(File.ReadAllText(JSONPath))
端函数
私有子SaveJSON(文件路径为字符串,对象为列表(MyFileObjects.MyFile))
File.WriteAllText(文件路径,MyFileObjects.Serialize(对象))
端接头
私有子TextBox1\u KeyDown(发送方作为对象,e作为KeyEventArgs)处理TextBox1.KeyDown
如果e.KeyValue=Keys,则输入
Dim selectedItem=myFiles.Find(函数(f)f.FileName.Equals(TextBox1.Text))
如果selectedItem不是空的,那么
MessageBox.Show(选择editem.Title)
如果结束
如果结束
端接头
末级
Private Sub buttonSaveJson_Click(发送者作为对象,e作为事件参数)处理buttonSaveJson。单击
SaveJSON(jsonPath,myFiles)
端接头
我计划在文本框中添加一个自动完成,但如果值存储在json文件中,而不是在自动完成源中手动逐个输入列表,我不知道这是否可行。然后,用户可以从包含“filename”值和与“title”值匹配的列表中选择一个。最后,“title”值将显示在消息框中。当我想从json文件中获取值时,您认为可以这样做吗?这里的错误是“e.KeyValue不是system.eventargs的成员”。这是否意味着我没有包含任何与其相关的名称空间元素?您订阅了错误的事件。TextBox.KeyDown
事件提供的是KeyEventArgs
,而不是EventArgs
。检查你的代码。我开始运行它并在文本框中输入一个字母,然后它显示:{“值不能为空。参数名称:source”}\u发布你现在使用的代码(更新你的问题)。你没听懂我在这里写的。如果我看不到你的实际代码是什么,我就不能告诉你它出了什么问题。如果你不知道如何更新你的问题,那就是它下面的edit
链接。你没有在我能看到的任何地方定义myFiles
。我从未使用过TextBox.TextChanged
。您肯定不想在该事件中创建CustomSource。我将把不同的部分放在一个新的编辑中。尝试按说明插入所有内容。
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
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