我正在开发一个 API 包装器,但在反序列化空 JSON 对象时遇到了一些麻烦。
API 返回此 JSON 对象。注意空对象entities
:
{
"object": "page",
"entry": [
{
"id": "1158266974317788",
"messaging": [
{
"sender": {
"id": "some_id"
},
"recipient": {
"id": "some_id"
},
"message": {
"mid": "mid.$cAARHhbMo8SBllWARvlfZBrJc3wnP",
"seq": 5728,
"text": "test",
"nlp": {
"entities": {} // <-- here
}
}
}
]
}
]
}
这是我等效的message
属性结构(已编辑):
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct TextMessage {
pub mid: String,
pub seq: u64,
pub text: String,
pub nlp: NLP,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct NLP {
pub entities: Intents,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Intents {
intent: Option<Vec<Intent>>,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Intent {
confidence: f64,
value: String,
}
Serde 的默认设置是反序列化Option
s,即None
, 和::serde_json::Value::Null
.