Read JSON Into A Rust Struct With Serde
Code
#![allow(dead_code)]
use serde::Deserialize;
use serde_json;
#[derive(Debug, Deserialize)]
struct Site {
title: String,
pages: Vec<Page>
}
#[derive(Debug, Deserialize)]
struct Page {
id: u32,
content: String
}
fn main() {
let source = r#"{
"title": "asdf",
"pages": [
{ "id": 12, "content": "quick fox"},
{ "id": 37, "content": "slow dog"}
]
}"#;
let data: Site = serde_json::from_str(source).unwrap();
println!("{:?}", data);
}
Results
Site { title: "asdf", pages: [Page { id: 12, content: "quick fox" }, Page { id: 37, content: "slow dog" }] }