如何使用serde_json从对象数组中反序列化单个字段?

时间:2020-07-06 10:41:01

标签: rust serde serde-json

我有一个JSON,其中一个键包含一个对象数组,我只对其中的一个字符串感兴趣:

{
    "actors": [{
        "name": "Bob",
        "foo": "bar"
    }, {
        "name": "Jim",
        "foo": "baz"
    }],
    "fruits": [{
        "name": "Pineapple",
        "foo": "bar"
    }, {
        "name": "Apple",
        "foo": "baz"
    }]
}

我有

use serde::Deserialize; // 1.0.110

#[derive(Deserialize)]
pub struct Name {
    pub name: String,
}

#[derive(Deserialize)]
pub struct Data {
    pub actors: Vec<Name>,
    pub fruits: Vec<Name>,
}

我该如何展平,以便拥有Vec<String>而不是Vec<Name>?例如我想结束

actors: ["Bob", "Jim"],
fruits: ["Pineapple", "Apple"],

0 个答案:

没有答案