【问题标题】:Mismatched types when trying to parse JSON from curl with serde_json尝试使用 serde_json 从 curl 解析 JSON 时类型不匹配
【发布时间】:2017-12-02 19:42:58
【问题描述】:

我已经从开放 WIFI 的 API 中获得 JSON 输出,我想将其放入数据库中。数据在this form

我已经通过curl获得了信息:

let mut easy = Easy::new();
easy.url("https://map.freifunk-rhein-neckar.de/data/nodes.json")
    .unwrap();
easy.write_function(|data| Ok(stdout().write(data).unwrap()))
    .unwrap();
easy.perform().unwrap();

let mut json = easy.response_code().unwrap();

我尝试使用 serde_json:

extern crate curl;
extern crate serde;
extern crate serde_json;

use std::io::{stdout, Write};
use curl::easy::Easy;
#[macro_use]
extern crate serde_derive;

use serde_json::Error;

#[derive(Serialize, Deserialize)]
struct Freifunk {
    timestamp: u32,
    version: i8,
    nodes: u32,
}

fn json_to_rust() -> Result<(), Error> {
    //Json von Homepage "Auslesen/Downloaden"
    let mut easy = Easy::new();
    easy.url("https://map.freifunk-rhein-neckar.de/data/nodes.json")
        .unwrap();
    easy.write_function(|data| Ok(stdout().write(data).unwrap()))
        .unwrap();
    easy.perform().unwrap();

    let mut json = easy.response_code().unwrap();

    let to_string: Freifunk = serde_json::from_value(json)?;
}

fn main() {}

我总是得到一个错误:

error[E0308]: mismatched types
  --> src/main.rs:29:54
   |
29 |     let to_string: Freifunk = serde_json::from_value(json)?;
   |                                                      ^^^^ expected enum `serde_json::Value`, found u32
   |
   = note: expected type `serde_json::Value`
              found type `u32`

error[E0308]: mismatched types
  --> src/main.rs:18:40
   |
18 |   fn json_to_rust() -> Result<(), Error> {
   |  ________________________________________^
19 | |     //Json von Homepage "Auslesen/Downloaden"
20 | |     let mut easy = Easy::new();
21 | |     easy.url("https://map.freifunk-rhein-neckar.de/data/nodes.json")
...  |
29 | |     let to_string: Freifunk = serde_json::from_value(json)?;
30 | | }
   | |_^ expected enum `std::result::Result`, found ()
   |
   = note: expected type `std::result::Result<(), serde_json::Error>`
              found type `()`

您能否举个例子说明如何处理数据以将其输入数据库?

【问题讨论】:

    标签: rust serde-json


    【解决方案1】:

    我已经通过 curl 获得了信息:

    不,你没有。您下载了它,然后将其写入标准输出:

    easy.write_function(|data| Ok(stdout().write(data).unwrap()))
    

    您所称的jsonHTTP response code。这是u32 类型的值:

    let mut json = easy.response_code().unwrap();
    

    将数据放入向量是described in the curl documentation。编译器告诉你你的类型不正确;您需要阅读并理解它,然后找出类型错误的原因:

       = note: expected type `serde_json::Value`
                  found type `u32`
    

    此外,您不能使用from_value,因为您没有可读取的serde_json::Value

    您的第二个错误是因为您已声明您的函数返回 Result,但您没有在函数结束时返回此类。你只是...停止,它返回一个()

       = note: expected type `std::result::Result<(), serde_json::Error>`
                  found type `()`
    

    extern crate curl;
    extern crate serde;
    #[macro_use]
    extern crate serde_derive;
    extern crate serde_json;
    
    use curl::easy::Easy;
    use serde_json::Error;
    
    #[derive(Debug, Serialize, Deserialize)]
    struct Freifunk {
        timestamp: u32,
        version: i8,
        nodes: u32,
    }
    
    fn json_to_rust() -> Result<(), Error> {
        let mut json = Vec::new();
    
        let mut easy = Easy::new();
        easy.url("https://map.freifunk-rhein-neckar.de/data/nodes.json")
            .unwrap();
        {
            let mut transfer = easy.transfer();
            transfer
                .write_function(|data| {
                    json.extend_from_slice(data);
                    Ok(data.len())
                })
                .unwrap();
            transfer.perform().unwrap();
        }
    
        assert_eq!(200, easy.response_code().unwrap());
    
        let freifunk: Freifunk = serde_json::from_slice(&json)?;
    
        println!("{:?}", freifunk);
    
        Ok(())
    }
    
    fn main() {}
    

    【讨论】:

      猜你喜欢
      • 2022-11-10
      • 1970-01-01
      • 2018-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      • 2015-02-03
      相关资源
      最近更新 更多