You are in luck, serde_json provides exactly the sort of magic you are looking for with serde_json::to_string and serde_json::from_str.
let mut responseVector: Vec<HashMap<String, String>> = Vec::new();
// serde_json::to_string will do all the work for us
let json: String = serde_json::to_string(&responseVector).unwrap();
println!("{:?}", json);
You can also derive serde::Serialize and serde::Deserialize for a struct so you do not need to deal with getting values out of a hashmap.
use serde::{Serialize, Deserialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct LoginInfo {
username: String,
email: String,
password: String,
}
let response = r#"[{"username": "Mario", "password": "itzzami", "email": "[email protected]"}, {"username": "Luigi", "password": "ayayayay", "email": "[email protected]"}]"#;
// We can also deserialize a struct which implements Deserialize.
let requests: Vec<LoginInfo> = serde_json::from_str(response).unwrap();
println!("Received {} requests!", requests.len());
for request in requests {
println!("{:?}", request);
}
playground link