You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rust Serde反序列化时sendAt字段始终为null的问题求助

Rust Serde反序列化时sendAt字段始终为null的问题求助

大家好,我最近碰到一个Serde反序列化的棘手问题,折腾了半天没找到原因,来请教下各位大佬。

我定义了一个Message结构体用来解析API返回的JSON数据,代码如下:

pub struct Message {
    pub body: String,
    #[serde(rename="sendAt")]
    pub send_at: Option<String>,
    pub is_internal: bool,
    pub sender: Sender,
    #[serde(deserialize_with = "deserialize_receiver")]
    pub receiver: Option<Receiver>,
    pub file: Option<String>
}

API返回的原始JSON里明明有"sendAt": "2025-03-18 17:11:26"这个字段,我也特意打印了原始响应确认它的存在,但反序列化之后,send_at字段却始终是None,其他字段都能正常解析出来,这就很奇怪了。

我的反序列化逻辑代码是这样的:

if status.is_success() {
    let request_text = match response.text().await {
        Ok(text) => {
            println!("Raw API Response: {}", text);
            text
        },
        Err(e) => return Err(ApiError::NetworkError(format!("Failed to read response: {}", e))),
    };
    match serde_json::from_str::<Vec<Message>>(&request_text) {
        Ok(messages) => Ok(messages),
        Err(e) => {
            println!("Deserialization error: {}", e);
            Err(ApiError::ParseError(format!("Failed to parse response: {}", e)))
        },
    }

我自己排查了半天,没发现哪里有问题,有没有朋友遇到过类似的情况?或者能给我一些排查的方向?

补充几个我已经试过的点(供参考)

  • 确认了原始JSON里的sendAt字段拼写和大小写完全正确;
  • 反序列化过程中没有报错,println!输出的错误信息是空的;
  • 其他字段(包括自定义反序列化的receiver)都能正常解析。

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.08 08:18:02