我想使用Alamofire将JSON数据转换为Swift中的Dictionary。
我正在尝试if let dict = response.result.value as? Dictionary<String, AnyObject> { ... }
,不幸的是它不起作用。有什么建议?提前致谢。这是打印我的response.result.value
Optional(<__NSArrayI 0x6000002893d0>(
{
category = category1;
description = description1;
name = sth1;
id = 1;
price = "213";
type = type1;
},
{
category = category2;
description = description2;
name = sth2;
id = 2;
price = "2133";
type = type4;
},
{
category = category3;
description = description3;
name = sth3;
id = 3;
price = "21334";
type = type5;
}
)
)
答案 0 :(得分:5)
因为您的响应是一个数组,所以您需要创建一个字典数组。只有词典不能用于您的上述回复。 所以,改变下面的代码。
if let dict = response.result.value as? [[String : AnyObject]]
{ ... }
答案 1 :(得分:1)
您需要逐步从Json中提取数据...... 你可以这样做。
if let arrayOfDic = response.result.value as? [Dictionary<String,AnyObject>]{
for aDic in arrayOfDic{
print(aDic)//print each of the dictionaries
if let price = aDic["price"] as? String{
print(price)//print price of each dic
}
}
}