如何在不需要包装类型的情况下反序列化JSON数组?

时间:2018-03-06 20:10:53

标签: json rust deserialization serde

我想反序列化以下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>,
}

1 个答案:

答案 0 :(得分:2)

直接反序列化向量:

let workspaces = serde_json::from_str::<Vec<Workspace>>(input);