我已经验证了编码,一切正常,直到我进入实际的解析函数,我不确定为什么我无法获取json文件数组中的任何值。我已经尝试了多种方法来获取数组中指定字符串中的每个对象,但仍然没有。这是我的示例代码。
[ { "Attack": 4, "Card Description": "<b>Deathrattle</b>Deal 2 damage to ALL other characters.", "Card Type": "0", "Class": "10", "Cost": 5, "Health": 4, "Name": "Abomination", "Rarity": 3 },
{ "Class": "1", "Name": "Fiery War Axe", "Cost": 2, "Card Type": 2, "Card Description": "", "Rarity": 5 }, ]
答案 0 :(得分:0)
如果您使用:
json["array"].array
您的JSON必须如下:
{
"array": [
{
"Attack": 4,
"Card Description": "DeathrattleDeal 2 damage to ALL other characters.",
"Card Type": "0",
"Class": "10",
"Cost": 5,
"Health": 4,
"Name": "Abomination",
"Rarity": 3
},
{
"Class": "1",
"Name": "Fiery War Axe",
"Cost": 2,
"Card Type": 2,
"Card Description": "",
"Rarity": 5
}
]
}
因为&#34;数组&#34;是数组的关键。
然后你可以使用:(json是你的JSON数组)
for obj in json["array"] {
println(obj["Name"].stringValue)
}