我得到了这个JSON:
{
cover = {
id = 1;
};
description = "Test"
place = {
id = 11;
location = {
city = Wheatley;
};
name = "Wheatley Provincial Park";
};
},
{
cover = {
id = 2;
};
description = "Cool"
place = {
id = 22;
location = {
city = Wheatley;
};
name = "Wheatley Provincial Park";
};
}
这是我的代码:
if let fbData = result as? [String : Any] {
print(fbData)
for events in fbData {
print (events["name"] as! String)
//this displays an error
//Type (Key: String, value: Any) has subscript members
}
}
但我不知道如何循环使用它们
我已经尝试过这些解决方案,但它们从未奏效过:
答案 0 :(得分:7)
if let array = result as? [String : AnyObject]{
if let fbData = array["data"] as? [[String : AnyObject]] {
print(fbData)
for event in fbData {
print (event["name"] as! String)
}
}
}
result
属于Any
类型[String : AnyObject]
data
并投射到字典数组 - [[String : AnyObject]]
。