如何将 actix_web::web::Bytes 转换为 json?

问题描述

这是我的代码

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let mut client = Client::default();

    // Create request builder and send request
    let response = client
        .get("https://ipapi.co/localhost/json")
        .header("User-Agent","actix-web/3.0")
        .send() // <- Send request
        .await; // <- Wait for response
    let result = response.unwrap().body().await.unwrap();
    println!("Response: {:?}",result);


}

代码有效,但我不知道如何将类型 actix_web::web::Byte 转换为 json。

我尝试将其转换为 json:

    let result = response
        .unwrap()
        .json::<HashMap<String,std::marker::PhantomData<String>>>()
        .await;

但是每次我收到错误时,因为 HashMap 具有混合类型。它是 HashMap

所以我的问题是:

如何将响应转换为 json?

我正在使用 use actix_web::client::Client

解决方法

您可以使用serde_json::value::Value

use serde_json::value::Value;

...

let result = response
        .unwrap()
        .json::<HashMap<String,Value>>()
        .await;