1

我想反序列化以下 JSON:

[
  {
    "name": "one",
    "path": "/path/to/one"
  },
  {
    "name": "two",
    "path": "/path/to/two"
  },
  {
    "name": "three",
    "path": "/path/to/three"
  }
]

进入一个Vec<Worskpace>. Workspace定义如下:

#[derive(Serialize, Deserialize)]
struct Workspace {
    name: String,
    path: String,
}

有没有办法做到这一点而不必做类似的事情:

#[derive(Serialize, Deserialize)]
struct Workspacesss {
    values: Vec<Workspace>,
}
4

1 回答 1

4

只需直接反序列化向量:

let workspaces = serde_json::from_str::<Vec<Workspace>>(input);
于 2018-03-06T20:21:18.333 回答