java如何在找到某个条目名称时转换/修改映射列表中的嵌套映射?
例如: 这是它在yaml结构中的外观:
states:
entries:
NONE:
value: core.lookups.states.none
readonly: true
MINOR:
value: core.lookups.states.minor
readonly: true
MAJOR:
value: core.lookups.states.major
readonly: true
readonly: true
这是转换后的外观:
states:
entries:
- key: "NONE"
value: core.lookups.states.none
readonly: true
- key: "MINOR"
value: core.lookups.states.minor
readonly: true
- key: "MAJOR"
value: core.lookups.states.major
readonly: true
readonly: true
注意:可能有更多条目,但每个条目中的键“条目”都是相同的,如:
states:
entries:
- key: "NONE"
value: core.lookups.states.none
readonly: true
- key: "MINOR"
value: core.lookups.states.minor
readonly: true
- key: "MAJOR"
value: core.lookups.states.major
readonly: true
readonly: true
states2:
entries:
- key: "NONE"
value: core.lookups.states2.none
readonly: true
- key: "MINOR"
value: core.lookups.states2.minor
readonly: true
- key: "MAJOR"
value: core.lookups.states2.major
readonly: true
readonly: true
注意:可能有更多条目,但每个条目中的键“条目”都是相同的,如:
public static void hashMapper(Map<String, Object> lookups) throws ParseException
{
for (Map.Entry<String, Object> entry : lookups.entrySet())
{
String key = entry.getKey();
Object value = entry.getValue();
if (key.equals("entries"))
{
//modify the entry into list
}
else if (value instanceof Map)
{
Map<String, Object> subMap = (Map<String, Object>) value;
hashMapper(subMap);
}
else
{
throw new IllegalArgumentException(String.valueOf(value));
}
}
}
这就是我到目前为止所做的。每次当我找到带有条目值的键时,我都应该转换里面的条目。各州也可以有另一个父密钥
# 1 楼答案
下面是如何使用Jackson提供的
YAMLFactory
的快速演示:将打印:
我使用了这些依赖项:
# 2 楼答案
以下是一个可能的解决方案:
更新:就地转换