我已经尝试了this解决方案,但它不工作.
我总是得到错误Error("missing field key")
,并想知道如何修复这个问题。
代码:
pub fn get_user(id: String) -> Data {
let cache: RedisCache = RedisCache;
let s: String = cache.get_str(&id).unwrap();
println!("{}", &s);
let user: Data = serde_json::from_str(&s).unwrap();
user
}
id = 40:a3:cc:a3:dc:43
返回:
{"change":"new","data":[],"token":"2a256356"}
此输出是正确的,但将此String
反序列化为Data
时出现错误。
数据如下所示:
#[derive(Serialize, Deserialize, ToSchema, Clone)]
#[serde(crate = "rocket::serde")]
pub struct Data {
/// Device's MAC address.
#[schema(example = "40:a3:cc:a3:dc:43")]
pub key: String,
/// Data from a device.
#[schema(value_type = Object,
example = "{ token: 2a256356, change: new, data: [ { dns: www.google.de, mac: 40:a3:cc:a3:dc:43, src: 10.42.0.177, dst: 10.42.0.1} ] }",
)]
pub value: Vec<Value>,
/// Indication whether task is done or not.
pub done: bool,
}
我正在使用utoipa和rocket 0.5
。
1条答案
按热度按时间1yjd4xko1#
我通过创建
Data
而不是使用from_str()
修复了它: