Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
异步解析来自请求GET调用的JSON响应_Json_Http_Rust - Fatal编程技术网

异步解析来自请求GET调用的JSON响应

异步解析来自请求GET调用的JSON响应,json,http,rust,Json,Http,Rust,我正在对某个API进行GET调用,该API将返回JSON对象列表。但是,我无法将其解析为自定义数据结构列表 离我最近的地方 struct Pokemon { id: i32, name: String, height: i32, weight: i32, } let mut response = client.get("http://pokeapi.co/api/v2/pokemon/111") .send() .expect("Failed t

我正在对某个API进行GET调用,该API将返回JSON对象列表。但是,我无法将其解析为自定义数据结构列表

离我最近的地方

struct Pokemon {
    id: i32,
    name: String,
    height: i32,
    weight: i32,
}

let mut response = client.get("http://pokeapi.co/api/v2/pokemon/111")
    .send()
    .expect("Failed to send request");
if let Ok(pokemon) = response.json::<Pokemon>() {
    println!("{:#?}", pokemon);
}
为了使用Response::json,您必须为Pokemon实现serde::反序列化。您可以通过在[dependencies]下的Cargo.toml中添加以下内容来实现此目的

然后,添加use-serde::反序列化;在文件顶部,将口袋妖怪的声明更改为:

let url = url.parse().expect("API URL parsing bug");
let request = Request::new(reqwest::Method::GET, url);

self.inner
    .execute(request)
    .map_err(Error::Request)
    .and_then(move |response: Response| {
        ...
    })
serde = { version = "1.0", features = ["derive"] }
#[derive(Deserialize)]
struct Pokemon {
    id: i32,
    name: String,
    height: i32,
    weight: i32,
}