VB.NET WPF - 使用 JSON 数组中的值填充组合框
VB.NET WPF - Populate Combobox with values from a JSON array
我在本地文件中有一个 json 数组,如下所示 (local.json)
[
{
"Name": "Element1",
"Url": "https://someurlforelement1.com"
},
{
"Name": "Element1",
"Url": "https://someurlforelement2.com"
}
]
对应class在vb
Public Class MyElement
Public Property Name As String
Public Property Url As String
End Class
我试图在应用程序开始时读取 json 文件,并用 MyElement 对象列表填充一个组合框。
目标是显示名称 属性,但使用对象的 Url 属性 作为值。
谷歌搜索后我尝试了一些事情,但我对 VB.NET 还很陌生,不得不接手其他开发人员的工作,因此需要帮助:) 谢谢
编辑:这是从下面提供的答案开始的最终代码:
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
' Read json and setup the combobox
Dim rawjson = File.ReadAllText(My.Application.Info.DirectoryPath & "\local.json")
Dim lst = JsonConvert.DeserializeObject(Of List(Of MyElement))(rawjson)
For Each i In lst
filterComboBox.Items.Add(i)
Next
filterComboBox.DisplayMemberPath = "Name"
filterComboBox.SelectedValuePath = "Url"
通过使用它,您可以获得 class 对象的列表
但为此你需要导入 Newtonsoft.Json
Dim rawjson = File.ReadAllText(My.Application.Info.DirectoryPath & "\local.json")
Dim lst = JsonConvert.DeserializeObject(Of List(Of MyElement))(rawjson)
现在使用此列表创建循环并填充您的组合框
我在本地文件中有一个 json 数组,如下所示 (local.json)
[
{
"Name": "Element1",
"Url": "https://someurlforelement1.com"
},
{
"Name": "Element1",
"Url": "https://someurlforelement2.com"
}
]
对应class在vb
Public Class MyElement
Public Property Name As String
Public Property Url As String
End Class
我试图在应用程序开始时读取 json 文件,并用 MyElement 对象列表填充一个组合框。 目标是显示名称 属性,但使用对象的 Url 属性 作为值。 谷歌搜索后我尝试了一些事情,但我对 VB.NET 还很陌生,不得不接手其他开发人员的工作,因此需要帮助:) 谢谢
编辑:这是从下面提供的答案开始的最终代码:
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
' Read json and setup the combobox
Dim rawjson = File.ReadAllText(My.Application.Info.DirectoryPath & "\local.json")
Dim lst = JsonConvert.DeserializeObject(Of List(Of MyElement))(rawjson)
For Each i In lst
filterComboBox.Items.Add(i)
Next
filterComboBox.DisplayMemberPath = "Name"
filterComboBox.SelectedValuePath = "Url"
通过使用它,您可以获得 class 对象的列表 但为此你需要导入 Newtonsoft.Json
Dim rawjson = File.ReadAllText(My.Application.Info.DirectoryPath & "\local.json")
Dim lst = JsonConvert.DeserializeObject(Of List(Of MyElement))(rawjson)
现在使用此列表创建循环并填充您的组合框