LoginSignup
0
0

More than 1 year has passed since last update.

APIアプリ

Posted at

1,WebAPIの仕組み

YahooのDeveloper Network
天気情報を提供するAPI

Yahoo Weather API for your apps

WebAPIによるデータ取得

  • HTTPリクエスト アプリからサーバーを呼び出す
  • HTTPレスポンス  JSON,XML

今回はJSONで受けとりデータのパース処理をする

2,HTTPリクエスト

let url = "\(url)"
getURL(url:url)

JSONデータを取得するコーdp

func getURL(url:String){

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]
        //取得したデータをキーと値で表示できる [Stringがキー Anyが値]
            print(json)
        } catch{
            self.sunriseTimeLabel.text = "取得できませんでした"
        }
    }
}

外部通信を許可しないと外部のサーバーへの通信ができない。

4,外部通信を許可する

プロジェクトの設定を変更
Info.plist
二つを追加する
スクリーンショット 2022-10-12 23.36.25.png

5,データを取り出す

query:{ results:{channel:{}}中身}を取り出す

let query = json["query"] as! [String:Any]
let results = query["results"] as! [String:Any]
            let channel = results["channel"] as! [String:Any]
            let astonomy = channel["astoronomy"] as! [String:Any]
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0