2016-11-20 10 views
0

を動作していないここに私のコードだ:スウィフト3のREST APIは、スウィフト3で休憩APIに問題が

func getData() 
{ 
    let urlString = "http://dataservice.accuweather.com/locations/v1/regions?apikey=QVU3TATgJEdRyojFze6zivdrmiln9XlA" 

    let config = URLSessionConfiguration.default // Session Configuration 
    let session = URLSession(configuration: config) // Load configuration into Session 
    let url = URL(string: urlString)! 

    let task = session.dataTask(with: url, completionHandler: { 
     (data, response, error) in 

     if error != nil { 

      print(error!.localizedDescription) 

     } else { 

      do { 
       print("in do block") 
       if let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any] 
       { 
        print("if condition is true") 
        //Implement your logic 
        print(json) 

       } else 
       { 
        print("Error in json serial") 
       } 

      } catch { 

       print("error in JSONSerialization") 

      } 


     } 

    }) 
    task.resume() 
} 

実行した後、私は、次のprint文を見ることができます:

息子に エラーをブロックしないでシリアル

ここでJSONシリアル化コマンドの何が問題なのかわかりません。期待される応答はJSON配列です。どんな助けもありがとう。あなたが持っているJSON beacuse

if let json = try JSONSerialization.jsonObject(with: data!, options:[]) as? [Any] 

にTHX

+0

シリアル化しようとする前に 'data'の値を印刷してみてください。 –

+0

'data'は問題ありません。文字列に変換して出力します。私のjsonの文字列を見ることができます。 – Sri

+0

オプションを '.mutableContainers'に変更してみてください。また、これを見てください:http://stackoverflow.com/a/39520853/4656341 – damiancesar

答えて