最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

rust - "invalid type: map, expected a sequence" when deserialize a JSON structure with serde_json - Stack Over

programmeradmin1浏览0评论

I can't catch the error cause. I have used the same procedure with other similar commands using the following tool: transform JSON to RUST

Another post takes the problem, but a don't understand the solution, I'm still very new to Rust.

Fragment of the main():

    match get_account_snapshot(&cfg.api_config.api_key, &cfg.api_config.secret_key).await {
        Ok(snapshot) => log::info!("{:#?}", snapshot),
        Err(e) => log::info!("{:#?}", e),
    }; 

Structures and function get_account_snapshot:

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AccountSnapshot {
    pub code: i64,
    pub msg: String,
    #[serde(rename = "snapshotVos")]
    pub snapshot_vos: Vec<SnapshotVo>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SnapshotVo {
    #[serde(rename = "type")]
    pub type_field: String,
    #[serde(rename = "updateTime")]
    pub update_time: i64,
    pub data: Data,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Data {
    #[serde(rename = "totalAssetOfBtc")]
    pub total_asset_of_btc: String,
    pub balances: Vec<Balance>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Balance {
    pub asset: String,
    pub free: String,
    pub locked: String,
}

pub async fn get_account_snapshot(api_key: &str, api_secret: &str) -> Result<Vec<AccountSnapshot>, BError> {
    let credentials = Credentials::from_hmac(api_key, api_secret);
    let client = BinanceHttpClient::default().credentials(credentials);
    let request = wallet::account_snapshot("SPOT");
    let data = client.send(request).await.map_err(BError::Client)?.into_body_str().await.map_err(BError::Client)?;
    log::info!("Before deserialize ------> {}", data);
    let data = serde_json::from_str::<Vec<AccountSnapshot>>(data.as_str()).map_err(BError::SerdeJson)?;
    // log::info!("{:#?}", data);
    Ok(data)
}

Terminal output:

I can't catch the error cause. I have used the same procedure with other similar commands using the following tool: transform JSON to RUST

Another post takes the problem, but a don't understand the solution, I'm still very new to Rust.

Fragment of the main():

    match get_account_snapshot(&cfg.api_config.api_key, &cfg.api_config.secret_key).await {
        Ok(snapshot) => log::info!("{:#?}", snapshot),
        Err(e) => log::info!("{:#?}", e),
    }; 

Structures and function get_account_snapshot:

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AccountSnapshot {
    pub code: i64,
    pub msg: String,
    #[serde(rename = "snapshotVos")]
    pub snapshot_vos: Vec<SnapshotVo>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SnapshotVo {
    #[serde(rename = "type")]
    pub type_field: String,
    #[serde(rename = "updateTime")]
    pub update_time: i64,
    pub data: Data,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Data {
    #[serde(rename = "totalAssetOfBtc")]
    pub total_asset_of_btc: String,
    pub balances: Vec<Balance>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Balance {
    pub asset: String,
    pub free: String,
    pub locked: String,
}

pub async fn get_account_snapshot(api_key: &str, api_secret: &str) -> Result<Vec<AccountSnapshot>, BError> {
    let credentials = Credentials::from_hmac(api_key, api_secret);
    let client = BinanceHttpClient::default().credentials(credentials);
    let request = wallet::account_snapshot("SPOT");
    let data = client.send(request).await.map_err(BError::Client)?.into_body_str().await.map_err(BError::Client)?;
    log::info!("Before deserialize ------> {}", data);
    let data = serde_json::from_str::<Vec<AccountSnapshot>>(data.as_str()).map_err(BError::SerdeJson)?;
    // log::info!("{:#?}", data);
    Ok(data)
}

Terminal output:

Share Improve this question edited Mar 15 at 1:46 Hilory 2,1377 gold badges14 silver badges30 bronze badges asked Mar 15 at 1:13 rabolonrabolon 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 1

It looks like you're attempting to deserialize a Vec directly from a JSON map, which won't work.

I would attempt to deserialize AccountSnapshot directly, without the Vec warparound.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论