Last active
October 31, 2016 14:17
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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