OpenWeatherMapより以下のJSONデータを取得しました。
JSONデータ*******************************
{
"coord”:{
“lon":133.93,
"lat":34.8
},
"weather":[{"id":801,"main":"Clouds","description":"薄い雲","icon":"02d"}],
"base":"stations",
"main":{
"temp":11.6,
”pressure":1022,
"humidity":43,
"temp_min":11,
"temp_max":12
},
"visibility":10000,
"wind":{
"speed":2.6,
"deg":270
},
"clouds":{"all":20},
"dt":1519700400,
"sys":{
"type":1,
"id":7575,
"message":0.0067,
"country":"JP",
"sunrise":1519680993,
"sunset":1519721879
},
"id":1860293,
"name":"Kanagawa",
"cod":200
}
実際に欲しいデータは
・"weather"の"main"
・"main"の"temp"、"humidity"、"temp_min"、"temp_max"
・"sys"の"sunrise"、"sunset"
なのですが、"weather"の"main"は配列になっていてどのようにすれば良いのかよくわかりません。
①まず構造体を定義する辺り、特に配列の部分がどうしてよいのか分かりません。
struct Main : Coddle { let temp : Double let pressure : Int let humidity : Int let temp_min : Double let temp_max : Double } struct Sys : Codable { let sunrise : Int let sunset : Int } struct Weather : Codable { //①配列で分からないところです }
②ボタンを押したらまずは、情報をprint()しようと思っていますが、
上手くできません。
@IBAction func getInfoBT(_ sender: Any) { let listUrl = "https://api.openweathermap.org/data/2.5/weather?q=Kanagawa,jp&lang=ja&units=metric&APPID=xxxxxxxxxxxx" //listUrlの値を文字列にする? let apiURL = URL(string: listUrl)! let myData = try! Data(contentsOf: apiURL) let myWeather = try! JSONDecoder().decode(Weather.self, from: myData) let myMain = try! JSONDecoder().decode(Main.self, from: myData) let mySys = try! JSONDecoder().decode(Sys.self, from: myData) print("天気予報: (myWeather.main)") print("湿度: (myMain.humidity)") print("現在気温: (myMain.temp)") print("最低気温: (myMain.temp_min)") print("最高気温: (myMain.temp_max)") print("日の出: (mySys.sunrise)") print("日の入: (mySys.sunset)")
myWeather、myMain、mySysがnilになっています。
御教授お願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/02/27 07:25
2018/02/27 08:02
2018/02/28 04:39
2018/02/28 07:18
2018/02/28 08:00
2018/02/28 08:18
2018/03/01 01:11
2018/03/06 07:55
2018/03/06 09:13 編集
2018/03/07 00:59
2018/03/07 03:15
2018/03/07 04:22
2018/03/07 04:34
2018/03/07 06:43