extern crate openweather;
use openweather::LocationSpecifier;
static API_KEY: &str = "e85e0a3142231dab28a2611888e48f22";
fn main() {
let loc = LocationSpecifier::Coordinates {
lat: 24.87,
lon: 67.03,
};
let weather = openweather::get_current_weather(loc, API_KEY).unwrap();
print!(
"Right now in Minneapolis, MN it is {}K",
weather.main.humidity
);
}
错误 : 线程 'main' 在 ' call
Result::unwrap()
on anErr
value 时惊慌失措:ErrorReport { cod: 0, message: "Got unexpected response: \"{\\"coord\\":{\\"lon\\":67.03,\ \"lat\\":24.87},\\"weather\\":[{\\"id\\":803,\\"main\\":\\"Clouds\\",\\"description \\":\\"碎云\\",\\"icon\\":\\"04n\\"}],\\"base\\":\\"车站\\",\\" main\\":{\\"temp\\":294.15,\\"压力\\":1018,\\"湿度\\":60,\\"temp_min\\":294.15,\\"temp_max \\":294.15},\\"能见度\\":6000,\\"wind\\":{\\"speed\\":5.1,\\"deg\\":30},\\"云\\":{\\"all\\":70},\\"dt\\":1574012543,\\"sys\\":{\\"type\\":1,\\"id\\":7576,\\"country\\":\\"PK\\",\\"sunrise\\":1573955364,\\"日落\\":1573994659},\\"timezone\\":18000,\\"id\\":1174872,\\"name\\":\\"Karachi\\",\\"cod\\ ":200}\"" }
问题是由于反序列化结构与 OpenWeather 的 JSON 不匹配而导致 JSON 解析错误,也许 API 最近添加了这个?在您的示例中,缺少OpenWeatherCurrent结构timezone
。
但是看起来有一个开放的 PR可以解决这个问题,您可以通过执行以下操作来测试它:
Cargo.toml
依赖项更改为openweather = { git = "https://github.com/caemor/openweather" }
.PR 作者还更新了get_current_weather
签名,因此您需要将第 2、10 行更改为以下内容:
use openweather::{LocationSpecifier, Settings};
let weather = openweather::get_current_weather(&loc, API_KEY, &Settings::default()).unwrap();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句