提问者:小点点

我试图通过Rust中的OpenWeather API获取数据,但我想我在解析方面遇到了一些困难


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”在调用<code>Result::unwrap()


共1个答案

匿名用户

问题是由于反序列化结构与OpenWeather的JSON不匹配而导致的JSON解析错误,也许API最近添加了这个?在您的示例中,OpenWeatherCurrent 结构缺少时区

但看起来有一个开放的PR可以解决这个问题,您可以通过执行以下操作来测试它:

>

  • 更改您的货物。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();