我想name
在层次结构的深处打印对象中每个联系人的 。联系人对象可能不会每次都具有完全相同数量的字段来构建合适的结构。我怎样才能做到这一点?
extern crate serde_json;
use serde_json::{Error, Value};
use std::collections::HashMap;
fn untyped_example() -> Result<(), Error> {
// Some JSON input data as a &str. Maybe this comes from the user.
let data = r#"{
"name":"John Doe",
"age":43,
"phones":[
"+44 1234567",
"+44 2345678"
],
"contact":{
"name":"Stefan",
"age":23,
"optionalfield":"dummy field",
"phones":[
"12123",
"345346"
],
"contact":{
"name":"James",
"age":34,
"phones":[
"23425",
"98734"
]
}
}
}"#;
let mut d: HashMap<String, Value> = serde_json::from_str(&data)?;
for (str, val) in d {
println!("{}", str);
if str == "contact" {
d = serde_json::from_value(val)?;
}
}
Ok(())
}
fn main() {
untyped_example().unwrap();
}
我对 Rust 非常陌生,基本上来自 JavaScript 背景。