Swift3 API接続 !編集途中-読まないこと!

APIの接続についてまとめます。 ここがエンドポイントのようだ https://www.googleapis.com/calendar/v3/calendars/primary/events

ボタン押下時にエンドポイントのurlをget

 @IBAction func getSunrizeTime(_ sender: Any) {
        let url = "https://query.yahooapis.com/v1/public/yql?q=select%20astronomy.sunrise%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22\(cityNameInput.text!)%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"
        getURL(url: url)
        
    }

取得するurlを別関数で切り出す。

   func getURL(url: String){
        do{
            let apiURL = URL(string: url)!
            let data = try Data(contentsOf: apiURL)
            let json = try JSONSerialization.jsonObject(with: data) as! [String:Any]
            print(json)
        }catch{
            self.sunTime.text = "can not connect"
        }
    }

ログの確認

["query": {
    count = 1;
    created = "2018-08-09T23:00:17Z";
    lang = "en-US";
    results =     {
        channel =         {
            astronomy =             {
                sunrise = "5:27 am";
            };
        };
    };
}]