假设我有一个动态集合,其中包含一组键值。
例如:
[
{
key: test1,
value: [
{
key:c1level1,
value: [
{
key:settings-key,
value: c1level2Val
},
{
key: types,
value: typesVal
}
]
}
]
},
{
key: test2,
value: [
{
key:c2level1,
value: [
{
key:settings-key,
value: c2level2Val
},
{
key: types,
value: typesVal
}
]
}
]
}
]
我想获取所有具有名为“ settings-key”和“ types”的键的对象,并将它们保存在一个列表中。例如,
我该如何实现?
我尝试了此方法,但不适用于此解决方案。当它深入一个节点时,另一个节点留在外面,并且永远不会迭代。 例如,当迭代开始并且假设它在“ test1”上时,它将忽略“ test2”节点,而仅对“ test1”节点有效。这已经是一个问题:
foreach (var item in yaml)
{
if (item.Key == "settings-key" || item.Key == "types")
{
Type typeOfYaml = yaml.GetType();
string placeholderName = String.Empty;
bool placeholderNameExist = typeOfYaml.GetProperties().Where(p => p.Name.Equals("settings-key")).Any();
if (placeholderNameExist)
{
placeholderName = yaml["settings-key"];
}
else
{
return null;
}
List<object> types = null;
bool typesExist = typeOfYaml.GetProperties().Where(p => p.Name.Equals("types")).Any();
if (typesExist)
types = yaml["types"];
var data = new KeyValue
{
RowKey = placeholderName,
Types = types != null ? types.Select(x=>x.ToString()).ToArray() : null
};
return data;
}
else
{
GetKeyValue(yaml[item.Key]);
}
}