使用来自JSON文件的数据填充组合框

时间:2014-10-18 14:40:50

标签: json vb.net winforms

我正在尝试构建我认为应该是一个非常专门格式化的JSON文件的相对简单的读者/编辑器,主要是为了防止用户弄乱语法。

Json格式是:

"Items": {
"defaultActionClass": "Weaponclass",
"ItemObjects": [
  {
    "Id": "M16",
    "Name": "EM16",
    "Description": "Some Description",
    "Icon": "Icons/pictures/1.png",
    "AnimationSet": "Set1",
    "Type": "Weapon",
    "EquipmentClass": "Hands",
    "SlotsCount": 2,
    "Weight": 1,
    "AP": 1,
    "BurstFire": 1,
    "AutoFire": 0,
    "EffectiveRange": 8.0,
    "MuzzleVelocity": 2.5,
    "BaseAccuracy": 100.0,
    "Mobility": 6.0,
    "Damage": 30,
    "DamageFalloff": 30,
    "AmmoClips": [
      "AmmoClip556NATOx30"
    ]
  },

我可以在字符串中读到,这不是问题,我正在努力的是以下两个项目。

使用以下代码找到获取ID的方法:

Dim root As RootObject = JsonConvert.DeserializeObject(Of RootObject)(strjson)
Dim ID As List(Of String) = root.Items.ItemObjects.Select(Of String)(Function(tp) tp.Id).ToList()  

返回列表中的所有60个ID,现在我的挑战是将该列表放入组合框中。我知道我之前已经用其他项目做过这个,但是这个仍然无法实现。

平滑过度梳理组合件。

cmbWeaponID.DataSource = ID

好的,现在要弄清楚如何从组合框的SelectedIndexChanged中的每个ID中获取其余数据,并将其导入到不同的文本框中。

使用以下代码管理以实现这一目标:

        Dim o As JObject = JObject.Parse(strjson)
    Dim Type As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].Type"))
    Dim Name As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].Name"))
    Dim Description As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].Description"))
    Dim Icon As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].Icon"))
    Dim AnimationSet As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].AnimationSet"))
    Dim SlotsCount As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].SlotsCount"))
    Dim Weight As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].Weight"))
    Dim APCost As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].AP"))
    Dim BurstFire As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].BurstFire"))
    Dim AutoFire As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].AutoFire"))
    Dim EffectiveRange As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].EffectiveRange"))
    Dim MuzzleVelocity As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].MuzzleVelocity"))
    Dim BaseAccuracy As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].BaseAccuracy"))
    Dim Mobility As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].Mobility"))
    Dim Damage As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].Damage"))
    Dim DamageFalloff As String = CStr(o.SelectToken("Items.ItemObjects[" & cmbWeaponID.SelectedIndex & "].DamageFalloff"))

1 个答案:

答案 0 :(得分:0)

过了一段时间,所以我非常生疏,但是一旦我将JSON列入列表,我所需要的就是:

Dim ID As List(Of String) = root.Items.ItemObjects.Select(Of String)(Function(tp) tp.Id).ToList()
cmbWeaponID.DataSource = ID