2017-12-02 142 views
0

我从打开的WIFI的API获取了JSON输出,我想将其放入数据库。数据在this form尝试使用serde_json从curl中解析JSON时出现不匹配的类型

我已经通过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 `()` 

可不可以给我一个如何处理数据以将其存入数据库的示例?

回答

0

I already got the information via 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() {} 
相关问题