我从开放式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 `()`
您能举例说明如何处理数据以将其导入数据库吗?
答案 0 :(得分:1)
我已经通过curl获得了信息:
不,你没有。您下载了它,但随后将其写入标准版:
easy.write_function(|data| Ok(stdout().write(data).unwrap()))
您所谓的json
是HTTP 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() {}