Home
Head's Up: I'm in the middle of upgrading my site. Most things are in place, but there are something missing and/or broken including image alt text. Please bear with me while I'm getting things fixed.

Read JSON Into A Rust Struct With Serde

rust
//! ```cargo
//! [dependencies]
//! serde_json = "1.0.110"
//! serde = { version = "1.0.194", features = ["derive"] }
//! ```

#![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 start

Footnotes And References