问题
I want to deserialize the following JSON:
[
{
"name": "one",
"path": "/path/to/one"
},
{
"name": "two",
"path": "/path/to/two"
},
{
"name": "three",
"path": "/path/to/three"
}
]
Into a Vec<Worskpace>
. Workspace
is defined below:
#[derive(Serialize, Deserialize)]
struct Workspace {
name: String,
path: String,
}
Is there a way to do that without having to do something like:
#[derive(Serialize, Deserialize)]
struct Workspacesss {
values: Vec<Workspace>,
}
回答1:
Just deserialize the vector directly:
let workspaces = serde_json::from_str::<Vec<Workspace>>(input);
来源:https://stackoverflow.com/questions/49139196/how-do-i-deserialize-a-json-array-without-needing-a-wrapper-type