【问题标题】:I am trying to get data over an OpenWeather API in Rust but I am facing some iusse regarding parsing I guess我正在尝试通过 Rust 中的 OpenWeather API 获取数据,但我想我在解析方面遇到了一些问题
【发布时间】:2020-03-13 04:22:24
【问题描述】:
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
    );
}

错误:线程“主”在“调用Result::unwrap()”时出现恐慌 Err 值:ErrorReport { cod:0,消息:“得到意外响应: \"{\\"坐标\\":{\\"lon\\":67.03,\\"纬度\\":24.87},\\"天气\\":[{\\"id\\" :803,\\"main\\":\\"云\\",\\"说明\\":\\"破 云\\",\\"icon\\":\\"04n\\"}],\\"base\\":\\"stations\\",\\"main\\":{\\ "temp\\":294.15,\\"压力\\":1018,\\"湿度\\":60,\\"temp_min\\":294.15,\\"temp_max\\":294.15},\ \"能见度\\":6000,\\"风\\":{\\"速度\\":5.1,\\"度数\\":30},\\"云层\\":{\\ "all\\":70},\\"dt\\":1574012543,\\"sys\\":{\\"type\\":1,\\"id\\":7576,\\ "国家\\":\\"PK\\",\\"日出\\":1573955364,\\"日落\\":1573994659},\\"时区\\":18000,\\"id\ \":1174872,\\"姓名\\":\\"卡拉奇\\",\\"鳕鱼\\":200}\"" }

【问题讨论】:

    标签: api rust rust-cargo rust-crates rustup


    【解决方案1】:

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

    但似乎有一个open PR 可以解决此问题,您可以通过执行以下操作对其进行测试:

    • 将您的 Cargo.toml 依赖项更改为 openweather = { git = "https://github.com/caemor/openweather" }
    • 公关作者还更新了get_current_weather 签名,因此您需要将第 2、10 行更改为以下内容:

      use openweather::{LocationSpecifier, Settings};
      
      let weather = openweather::get_current_weather(&loc, API_KEY, &Settings::default()).unwrap();
      
      

    【讨论】:

      猜你喜欢
      • 2020-11-09
      • 2020-12-24
      • 1970-01-01
      • 2013-04-27
      • 1970-01-01
      • 2021-10-16
      • 2010-11-09
      • 2022-12-30
      相关资源
      最近更新 更多