How do I deserialize a JSON array without needing

2019-09-01 15:33发布

This question already has an answer here:

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条回答
混吃等死
2楼-- · 2019-09-01 15:51

Just deserialize the vector directly:

let workspaces = serde_json::from_str::<Vec<Workspace>>(input);
查看更多
登录 后发表回答