How do I deserialize a JSON array without needing a wrapper type? [duplicate]

别来无恙 提交于 2019-12-11 15:11:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!