多层级嵌套类型的 json 会被解析为内置的 Map 类型(object),需要多次迭代解析。
use serde_json::{Map, Value};
fn main() {
let str = r#"{"title":"xxx", "content":"abc", "keyboard":{"name":"tom", "email":"aa@qq.com"}, "ctime": 111111}"#;
let parsed: Value = serde_json::from_str(str).unwrap();
let obj: &Map<String, Value> = parsed.as_object().unwrap();
for (key, val) in obj {
if key == "keyboard" {
for (k, v) in val.as_object().unwrap() {
println!("o:{}|{}", k, v);
}
}
}
println!("a:{:?}", obj);
}