我有一些像这样的JSON(截断):
{
"data": {
"1001": {
"id": 1001,
"plaintext": "Slightly increases Movement Speed",
"description": "<groupLimit>Limited to 1.</groupLimit><br><br><unique>UNIQUE Passive - Enhanced Movement:</unique> +25 Movement Speed<br><br><i>(Unique Passives with the same name don't stack.)</i>",
"name": "Boots of Speed",
"group": "BootsNormal"
},
"1004": {
"id": 1004,
"plaintext": "Slightly increases Mana Regen",
"description": "<stats><mana>+25% Base Mana Regen </mana></stats>",
"name": "Faerie Charm"
},
"1011": {
"id": 1011,
"plaintext": "Greatly increases Health",
"description": "<stats>+380 Health</stats>",
"name": "Giant's Belt"
},
"1018": {
"id": 1018,
"plaintext": "Moderately increases Critical Strike Chance",
"description": "<stats>+15% Critical Strike Chance</stats>",
"name": "Cloak of Agility"
}
}
}
如果我尝试仅从第一项获取名称,我会使用json("data")("3745")("name")
,输出结果为:name1
我想跳过这样的所有数字:json("data")("*")("name")
。
使用*
通配符不起作用,所以目前我正在使用这样的代码:
For i = 1001 To 4000
Try
Dim jsonname As String = json(i.ToString)("name")
ComboBox4.Items.Add(jsonname.ToString)
Catch ex As Exception
End Try
Next
如果它可以获得具有当前i
的项目(例如3745),则将其添加到ComboBox,然后前进到下一个i
(3746)。如果它无法获取当前值i
的项目,那么它会给出异常并前进到下一个i
(并且一直打开直到4000)。
这实际上有效,但是应用程序非常慢(请注意,有50多个条目,每个条目的随机ID在1001到4000之间,我无法更改JSON)。
答案 0 :(得分:0)
您似乎遇到的问题是JSON中每个项目的数字键。因为你不知道键是什么,除了它们介于1000和4000之间,你按顺序尝试每个可能的值,这最终会很慢(特别是因为你在循环中使用了try / catch)处理失误)。
幸运的是你不需要猜测。如果你正在使用Json.Net(我假设你是;你的问题不清楚)那么有一种简单的方法可以在不知道密钥名称的情况下迭代值。这是一个简短的演示:
Dim json As String = _
"{" + _
"""data"": {" + _
"""1001"": {" + _
"""id"": 1001," + _
"""plaintext"": ""Slightly increases Movement Speed""," + _
"""description"": ""<groupLimit>Limited to 1.</groupLimit><br><br><unique>UNIQUE Passive - Enhanced Movement:</unique> +25 Movement Speed<br><br><i>(Unique Passives with the same name don't stack.)</i>""," + _
"""name"": ""Boots of Speed""," + _
"""group"": ""BootsNormal""" + _
"}," + _
"""1004"": {" + _
"""id"": 1004," + _
"""plaintext"": ""Slightly increases Mana Regen""," + _
"""description"": ""<stats><mana>+25% Base Mana Regen </mana></stats>""," + _
"""name"": ""Faerie Charm""" + _
"}," + _
"""1011"": {" + _
"""id"": 1011," + _
"""plaintext"": ""Greatly increases Health""," + _
"""description"": ""<stats>+380 Health</stats>""," + _
"""name"": ""Giant's Belt""" + _
"}," + _
"""1018"": {" + _
"""id"": 1018," + _
"""plaintext"": ""Moderately increases Critical Strike Chance""," + _
"""description"": ""<stats>+15% Critical Strike Chance</stats>""," + _
"""name"": ""Cloak of Agility""" + _
"}," + _
"}" + _
"}"
Dim data As JObject = JObject.Parse(json)("data")
For Each item As JObject In data.Values
Console.WriteLine("id: " & item("id").ToString())
Console.WriteLine("name: " & item("name").ToString())
Console.WriteLine("plaintext: " & item("plaintext").ToString())
Console.WriteLine()
Next
输出:
id: 1001
name: Boots of Speed
plaintext: Slightly increases Movement Speed
id: 1004
name: Faerie Charm
plaintext: Slightly increases Mana Regen
id: 1011
name: Giant's Belt
plaintext: Greatly increases Health
id: 1018
name: Cloak of Agility
plaintext: Moderately increases Critical Strike Chance