我有一个带有来自Web服务器的响应的字典。在词典里面,我有3对键和值。其中一个是阵列。如何访问整个阵列?
我打印时
print(result["top10"]
我得到这样的东西
{
item = (
{
count = 233;
"engine_size" = "3,0";
"fuel_type" = Otto;
"vehicle_brand" = Audi;
"vehicle_type" = "A4_8K";
},
{
count = 107;
"engine_size" = "3,0";
"fuel_type" = Diesel;
"vehicle_brand" = Audi;
"vehicle_type" = "A6_4G0";
};
}
我希望数据如下:
top10[0].count == "223"
top10[0].count == "3,0"
top10[1].count == "107"
top10[1].count == "3,0"
等等
答案 0 :(得分:0)
你的阵列不是Top10。 Top10是一个字典,其中包含" item"列在里面。所以,你想要的数组实际上是"项目"数组,里面的top10字典。通过result["top10"]
的打印,我可以看到top10["item"]
是Top10中的一系列词典。
试试这样:
if let top10 = result["top10"] as? NSDictionary, let items = top10["item"] as? [NSDictionary] {
for item in items {
print(item["count"])
}
}
它应该工作:)