我正在尝试通过 Rust 中的 OpenWeather API 获取数据,但我想我在解析方面遇到了一些问题
I am trying to get data over an OpenWeather API in Rust but I am facing some iusse regarding parsing I guess
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
);
}
error : thread 'main' panicked at 'called Result::unwrap()
on an
Err
value: ErrorReport { cod: 0, message: "Got unexpected response:
\"{\"coord\":{\"lon\":67.03,\"lat\":24.87},\"weather\":[{\"id\":803,\"main\":\"Clouds\",\"description\":\"broken
clouds\",\"icon\":\"04n\"}],\"base\":\"stations\",\"main\":{\"temp\":294.15,\"pressure\":1018,\"humidity\":60,\"temp_min\":294.15,\"temp_max\":294.15},\"visibility\":6000,\"wind\":{\"speed\":5.1,\"deg\":30},\"clouds\":{\"all\":70},\"dt\":1574012543,\"sys\":{\"type\":1,\"id\":7576,\"country\":\"PK\",\"sunrise\":1573955364,\"sunset\":1573994659},\"timezone\":18000,\"id\":1174872,\"name\":\"Karachi\",\"cod\":200}\""
}
问题是 JSON 解析错误,因为反序列化结构与 OpenWeather 的 JSON 不匹配,也许 API 最近添加了这个?在您的示例中, OpenWeatherCurrent 结构缺失 timezone
.
但似乎有一个 open 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();
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
);
}
error : thread 'main' panicked at 'called
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\":\"broken clouds\",\"icon\":\"04n\"}],\"base\":\"stations\",\"main\":{\"temp\":294.15,\"pressure\":1018,\"humidity\":60,\"temp_min\":294.15,\"temp_max\":294.15},\"visibility\":6000,\"wind\":{\"speed\":5.1,\"deg\":30},\"clouds\":{\"all\":70},\"dt\":1574012543,\"sys\":{\"type\":1,\"id\":7576,\"country\":\"PK\",\"sunrise\":1573955364,\"sunset\":1573994659},\"timezone\":18000,\"id\":1174872,\"name\":\"Karachi\",\"cod\":200}\"" }
问题是 JSON 解析错误,因为反序列化结构与 OpenWeather 的 JSON 不匹配,也许 API 最近添加了这个?在您的示例中, OpenWeatherCurrent 结构缺失 timezone
.
但似乎有一个 open 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();