我已经从一个用于打开 WIFI 的 API 获得 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 `()`
你能给我一个如何处理数据以将其输入数据库的示例吗?