gpt4 book ai didi

loops - 如何遍历递归/嵌套 HashMap 中的所有键?

转载 作者:行者123 更新时间:2023-11-29 08:28:39 24 4
gpt4 key购买 nike

我正在寻找从 Vec<Vec<String>> 创建一个 HashMap<String, Value>Value 类型来自 serde_json

预期的输出是:

[
["name"]
["age"]
["phones"]
["phones", "0"]
["phones", "1"]
["friends"]
["friends", "0"]
["friends", "0", "name"]
["friends", "0", "age"]
["friends", "0", "phones"]
["friends", "0", "phones", "0"]
["friends", "0", "phones", "1"]
["friends", "0", "friends"]
["friends", "0", "friends", "0"]
["friends", "0", "friends", "0", "name"]
["friends", "0", "friends", "0", "age"]
["friends", "0", "friends", "0", "phones"]
["friends", "0", "friends", "0", "phones", "0"]
["friends", "0", "friends", "0", "friends"]
["friends", "0", "friends", "1"]
["friends", "0", "friends", "1", "name"]
["friends", "0", "friends", "1", "age"]
["friends", "0", "friends", "1", "phones"]
["friends", "0", "friends", "1", "phones", "0"]
["friends", "0", "friends", "1", "friends"]
["friends", "0", "friends", "2"]
["friends", "0", "friends", "2", "name"]
["friends", "0", "friends", "2", "age"]
["friends", "0", "friends", "2", "phones"]
["friends", "0", "friends", "2", "phones", "0"]
["friends", "0", "friends", "2", "friends"]
];

问题:如何遍历递归/嵌套 HashMap 中的所有键并匹配上面的期望输出?

use serde_json::{Result, Value};
use std::collections::HashMap;

fn deep_keys(data: HashMap<String, Value>) -> Vec<Vec<String>>{
let mut vec = Vec::new();
vec.push(Vec::new());

// logic

for (str, val) in data {
match &val {
Value::String(string) => vec[0].push(str),
Value::Number(Number) => vec[0].push(str),
Value::Bool(bool) => vec[0].push(str),
_ => {
let mut map = HashMap::new();
for (innerStr, innerVal) in val {
let mut map = HashMap::new();
map.insert(str, innerVal);
deep_keys(map);
}

}
};
}
vec
}

fn main() {
let data = r#"
{
"name": "John Doe",
"age": 43,
"phones": [
"+44 1234567",
"+44 2345678"
],
"friends": [
{
"name": "Jenn Doe",
"age": 15,
"phones": ["+44 1234567", "+44 2345678"],
"friends": [
{
"name": "Richard Carter",
"age": 20,
"phones": ["+1 1234567"],
"friends": []
},
{
"name": "Angus Yik",
"age": 21,
"phones": ["+1 1234567"],
"friends": []
},
{
"name": "Diana White",
"age": 24,
"phones": ["+1 12345673"],
"friends": []
}
]
}
]
}"#;
let v: HashMap<String, Value> = serde_json::from_str(data).expect("error");
println!("{:?}", deep_keys(v));
}

当我尝试使 deep_keys 递归时,它报告有关类型不匹配的错误,我不确定如何修复:

rror[E0277]: `serde_json::value::Value` is not an iterator
--> src/main.rs:17:45
|
17 | for (innerStr, innerVal) in val {
| ^^^ `serde_json::value::Value` is not an iterator
|
= help: the trait `std::iter::Iterator` is not implemented for `serde_json::value::Value`
= note: required by `std::iter::IntoIterator::into_iter`

最佳答案

use serde_json::{Value};
use std::fs;
//use std::path::Path;
use std::io::Read;

fn deep_keys(value: &Value, current_path: Vec<String>, output: &mut Vec<Vec<String>>) {
if current_path.len() > 0 {
output.push(current_path.clone());
}

match value {
Value::Object(map) => {
for (k, v) in map {
let mut new_path = current_path.clone();
new_path.push(k.to_owned());
deep_keys(v, new_path, output);

}
},
Value::Array(array) => {
for (i, v) in array.iter().enumerate() {
let mut new_path = current_path.clone();
new_path.push(i.to_string().to_owned());
deep_keys(v, new_path, output);
}
},
_ => ()
}
}

fn main() {
let mut output = vec![vec![]];
let current_path = vec![];
let mut file = fs::File::open("src/geo.json").unwrap();
let mut data = String::new();
file.read_to_string(&mut data).unwrap();

let value:Value = serde_json::from_str(&data).expect("error");
deep_keys(&value, current_path, &mut output);
println!("{:?}", output);
}

关于loops - 如何遍历递归/嵌套 HashMap 中的所有键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57274688/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com