- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Swift 4 中的 OpenWeatherMap API 构建一个简单的天气应用程序。我可以在简单的情况下解析 Json 数据,但是这个结构更复杂。
这是 API 返回的 Json 文件。
{"coord":{"lon":144.96,"lat":-37.81},"weather":[{"id":520,"main":"Rain","description":"light intensity shower rain","icon":"09n"}],"base":"stations","main":{"temp":288.82,"pressure":1019,"humidity":100,"temp_min":288.15,"temp_max":289.15},"visibility":10000,"wind":{"speed":4.1,"deg":200},"clouds":{"all":90},"dt":1544284800,"sys":{"type":1,"id":9548,"message":0.5221,"country":"AU","sunrise":1544208677,"sunset":1544261597},"id":2158177,"name":"Melbourne","cod":200}
我创建了一些结构来获取 Json 数据。
struct CurrentLocalWeather: Decodable {
let base: String
let clouds: Clouds
let cod: Int
let coord: Coord
let dt: Int
let id: Int
let main: Main
let name: String
let sys: Sys
let visibility: Int
let weather: [Weather]
let wind: Wind
}
struct Clouds: Decodable {
let all: Int
}
struct Coord: Decodable {
let lat: Double
let lon: Double
}
struct Main: Decodable {
let humidity: Int
let pressure: Int
let temp: Double
let tempMax: Int
let tempMin: Int
private enum CodingKeys: String, CodingKey {
case humidity, pressure, temp, tempMax = "temp_max", tempMin = "temp_min"
}
}
struct Sys: Decodable {
let country: String
let id: Int
let message: Double
let sunrise: UInt64
let sunset: UInt64
let type: Int
}
struct Weather: Decodable {
let description: String
let icon: String
let id: Int
let main: String
}
struct Wind: Decodable {
let deg: Int
let speed: Double
}
要使用这些数据,这是我编写的代码:
let url = "https://api.openweathermap.org/data/2.5/weather?q=melbourne&APPID=XXXXXXXXXXXXXXXX"
let objurl = URL(string: url)
URLSession.shared.dataTask(with: objurl!) {(data, response, error) in
do {
let forecast = try JSONDecoder().decode([CurrentLocalWeather].self, from: data!)
for weather in forecast {
print(weather.name)
}
} catch {
print("Error")
}
}.resume()
这应该在控制台中打印城市名称。不幸的是它打印错误。
最佳答案
你需要
let forecast = try JSONDecoder().decode(CurrentLocalWeather.self, from: data!)
print(forcast.name)
因为根是字典而不是数组
关于json - OpenWeatherMap 和 Swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53687617/
我正在使用Open Weather Map API用于天气预报。当前可用版本是 2.5这是 XML 片段 ... ... 这里是the API request . 有人知道降水量字
我正在尝试将 openweathermap 用于我的 python 应用程序,但存在问题。我对这个链接做了一个简单的请求:http://api.openweathermap.org/data/2.5/
我正在尝试实现一个 iphone 应用程序,并且我正在集成 OpenWeatherMap 来检索当前天气。但是,我注意到返回的数据不正确(偏离大约 39 华氏度)。 下面是我用来检索美国丹佛当前天气的
使用 openweathermap api,如何使用 javascript 获取某一天的平均温度。我知道我的能力有限,只能在接下来的 5 天内完成。我一直在寻找,但找不到一种方法来做到这一点,也没有看
我刚刚创建了一个帐户 OpenWeatherMap 我想通过城市 ID API 调用获取当前位置的天气: http://api.openweathermap.org/data/2.5/weather?
我试图从值中检索 Main 和 Description,但它没有显示在 UILabel 中。所有其他参数都显示良好。如温度、最高和最低温度、风速和风向。我在使用天气图标时也遇到困难,如果我的代码正确,
我正在使用 OpenWeatherMap 获取我的应用程序的天气数据。通过响应数据,openweather 正在发送一个天气图标代码,并且他们有一个非常小的代码的特定图标,但我想要一个更大的图标用于我
我正在开发一个 Android 应用程序,我需要我所在城市的当前天气及其低温/高温。有没有办法获取我所在城市的当前高温和低温? 我发现有最低/最高温度,但我认为这不是我要找的。如果 openweath
我正在使用 openweathermap.org api,它为我提供了错误的输出。 如果我点击 this url ,我得到以下输出: "{"coord":{"lon":-121.96,"lat":37
问题是当我获取 URL 来获取预测的 JSON 文件时,它给了我一个 40 对象的数组(每 3 小时预测一次,持续 5 天),现在我只想显示每日预报,而非每 3 小时预报。 我怎样才能用 JavaSc
我想要获取 openweathermap 图标。 https://openweathermap.org/weather-conditions和 http://samples.openweatherma
我正在开发一个项目,我正在创建一个用于浇灌植物的java应用程序。从网上获取天气信息并基于该输出的想法应该是我们的植物是否需要水。对于天气信息,我找到了一个API OpenWeatherMap,我尝试
我正在尝试按纬度和经度获取当前天气数据。这是我的 Python 代码的一部分: import requests def get_weather(lat, lon): return reques
我目前正在学习如何通过学习 Udacity 关于构建天气应用程序的 Android 类(class)来创建 Android 应用程序。一项要求是使用 http://api.openweathermap
我正在尝试根据我通过 .geolocation 访问的用户的经度和纬度来定义搜索城市 将经度和纬度添加到我的网址时出现此错误 cod : "400" message :
我正在尝试获取某些城市的天气信息(城市是动态变化的),我发现了这个 map API,它非常容易集成。但一旦我尝试过,即使我给出了另一个城市(例如慕尼黑)的经纬度,它也只显示莫斯科。 这是代码:
Weather : {{ weather.weather[0].description }
我正在尝试使用 Openweathermap 获取城市的天气信息使用此代码: func getWeatherByCity(city: String) { if let weatherReque
我想知道我是否可以使用 openweathermap api 调用一个请求来立即获取有关(今天和所有工作日)的数据!我试过了>> http://api.openweathermap.org/data/
我正在尝试使用 JS Fetch API 获取 5 天的天气预报,尽管我按照文档中的说明传递了 cnt=5,但我仍然只获取当前天气。我错过了什么吗? fetch('https://api.openwe
我是一名优秀的程序员,十分优秀!