我在代码中遇到麻烦,在尝试解析JSON数据(数组的每个数据,如应该如何完成)并尝试设置for in循环时,错误就出来了。这是我的代码
if let jsonDataArray = try? JSONSerialization.jsonObject(with: data!, options: [])
{
print(jsonDataArray)
var allStops = [busStops]()
for eachData in jsonDataArray
^
//this is where the error is located
{
if let jsonDataDictionary = eachData as? [String : AnyObject]
{
let eachStop = busStops(jsonDataDictiony: jsonDataDictionary)
}
}
}
答案 0 :(得分:4)
直接指定jsonDataArray的类型[[String: Any]]
并尝试这样做。
if let jsonDataArray = try? JSONSerialization.jsonObject(with: data!, options: []) as? [[String: Any]] {
for eachData in jsonDataArray {
let eachStop = busStops(jsonDataDictiony: jsonDataDictionary)
}
}