Skip to content

Instantly share code, notes, and snippets.

@matthewjberger
Last active October 31, 2016 14:17
Show Gist options
  • Save matthewjberger/28599ab7be0ba511ae2dca8bfe1b9dfd to your computer and use it in GitHub Desktop.
Save matthewjberger/28599ab7be0ba511ae2dca8bfe1b9dfd to your computer and use it in GitHub Desktop.
Example of parsing some nested json from a file using a command line argument in Rust
extern crate rustc_serialize;
use rustc_serialize::json::Json;
use std::fs::File;
use std::io::Read;
use std::env;
fn main() {
let args: Vec<String> = env::args().collect();
println!("I got {:?} arguments: {:?}.", args.len() - 1, &args[1..]);
if args[0].is_empty() {
std::process::exit(1);
}
let mut file = File::open(&args[1]).unwrap();
let mut data = String::new();
file.read_to_string(&mut data).unwrap();
let json = Json::from_str(&data).unwrap();
// println!("{}", Json::pretty(&json));
println!("{}", json.find_path(&["basics", "name"]).unwrap());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment