Read lists of JSON structures



examples/json/read-list-of-json/data.json
{ "name": "Foo", "number": 10 }
{ "name": "Bar", "number": 20 }

examples/json/read-list-of-json/src/main.rs
use serde::Deserialize;

#[derive(Deserialize, Debug)]
#[allow(dead_code)]
struct Thing {
    name: String,
    number: u32,
}

fn main() {
    let filename = "data.json";
    let content = std::fs::read_to_string(filename).unwrap();
    for row in content.split('\n') {
        if row.is_empty() {
            continue;
        }
        //println!("row: {row}");
        let data = serde_json::from_str::<Thing>(row).unwrap();
        println!("data: {data:#?}");
    }
}

data: Thing {
    name: "Foo",
    number: 10,
}
data: Thing {
    name: "Bar",
    number: 20,
}