"invalid type: map, expected a sequence" when deserializing a nested JSON structure with Serde
Have a look at this part of your JSON input data:
{ ... "items": [ { ... "title": "Stop setting $CARGO_HOME to its default value", ... } ]}
- The top-level data structure is a JSON map, so in Rust this will be represented as a struct. I will use your name
Obj
. - The top-level JSON map has a key called
"items"
so in Rust this will be a fielditems
inside theObj
struct. - The value of
"items"
in the map is a JSON array, so in Rust let's use a Vec. - Each element in the JSON array is a JSON map so in Rust we need a struct for those. We can call it
Issue
. - Each issue has a JSON key called
"title"
so this will be a fieldtitle
inside theIssue
struct. - The value of
"title"
is a JSON string so we can use Rust'sString
type for the field.
#[derive(Deserialize, Debug)]struct Obj { items: Vec<Issue>,}#[derive(Deserialize, Debug)]struct Issue { title: String,}fn main() { let j = /* get the JSON data */; let issues = serde_json::from_str::<Obj>(j).unwrap(); for i in issues.items { println!("{:#?}", i); }}