2016-09-21 87 views
1

當我嘗試解析JSON數據(數組中的每個數據,比如應該如何完成)並嘗試設置在循環中,錯誤出來了。這裏是我的代碼類型'任何'不符合協議'序列'

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) 

           } 

          } 

         } 

回答

2

指定jsonDataArray的類型直接[[String: Any]]並嘗試像這樣。

if let jsonDataArray = try? JSONSerialization.jsonObject(with: data!, options: []) as? [[String: Any]] { 
    for eachData in jsonDataArray { 
     let eachStop = busStops(jsonDataDictiony: jsonDataDictionary) 
    } 
} 
相關問題