我试图通过Rust中的OpenWeatherAPI获取数据,但我想我在解析方面遇到了一些困难
错误:线程“main”在调用时惊慌失措。我试图通过Rust中的OpenWeatherAPI获取数据,但我想我在解析方面遇到了一些困难,api,rust,rust-cargo,rust-crates,rustup,Api,Rust,Rust Cargo,Rust Crates,Rustup,错误:线程“main”在调用时惊慌失措。Result::unwrap() Errvalue:ErrorReport{cod:0,消息:“获得意外响应: \“{\\\\\\\”:{\\\\\\\\\”:67.03,\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”:[\\\\\\\\\\\\\\\\\\\\ “云云”云,“云”云”云,”和“图标”云”云”云”云,“图标”云”云”云”云”云”
Result::unwrap()
Err
value:ErrorReport{cod:0,消息:“获得意外响应:
\“{\\\\\\\”:{\\\\\\\\\”:67.03,\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”:[\\\\\\\\\\\\\\\\\\\\
“云云”云,“云”云”云,”和“图标”云”云”云”云,“图标”云”云”云”云”云”云”云”云”云”云”云”云”云”云”云”云”云”云”云”云”云”云”云”云”云”云”云”云”云”云”云”云”云”云”云”云”云”,基地”基地”基地”基地”基地”云”基地”站”站”站”,主“主要”云”主:::::::::::::\\\\\“主要”主要”主::::::::::::::\\\\\\\“温度温度:温度:温度:温度:温度:温度:294.15:温度:294.15,压力:::::::::::::::::::294.15;压力:压力:::::::::::::::\“id\”:7576、\“国家\\”:\“主键\\”\\“日出\\”:1573955364,“日落\\”:1573994659},“时区\\”:18000,\\“id \\”:1174872,\\“姓名\\”:\\“卡拉奇\\”,\\“鳕鱼\\”:200}”
}
问题是由于反序列化的结构与OpenWeather的JSON不匹配而导致JSON解析错误,可能是API最近添加了这个?在您的示例中,该结构缺少时区
但看起来有一个解决方案,您可以通过执行以下操作进行测试:
- 将您的
Cargo.toml
依赖项更改为openweather={git=”https://github.com/caemor/openweather“}
- 公关作者还更新了
get\u current\u weather
签名,因此您需要将第2行和第10行更改为以下内容:
使用openweather::{LocationSpecifier,Settings};
让weather=openweather::获取当前天气(&loc,API_键,&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
);
}