-
Open the Cargo.toml file that was generated earlier for you
- Under [dependencies], if you haven't done so already, add the following line:
serde_json = "1.0.8"
- If you want, you can go to the serde_json crates.io web page (https://crates.io/crates/serde_json) to check for the newest version and use that one instead
-
In the bin folder, create a file called dynamic_json.rs
-
Add the following code and run it with cargo run --bin dynamic_json:
1 #[macro_use]
2 extern crate serde_json;
3
4 use std::io::{self, BufRead};
5 use std::collections::HashMap;
6
7 fn main() {
8 // A HashMap is the same as a JSON without any schema
9 let mut key_value_map = HashMap::new();
10 let stdin = io::stdin();
11 println!("Enter a key and a value");
12 for input in stdin.lock().lines() {
13 let input = input.expect("Failed to read line");
14 let key_value: Vec<_> = input.split_whitespace().collect(...