问题描述
我想知道如何在 java 上的 yaml 中执行此操作:
admins:
test:
id: 1234
我只设法在没有“子字段”的情况下做到了,就像这样:
address: Star City
id.test: 19
name: John
department: Medical
使用此代码:
Map<String,Object> dataMap = new HashMap<>();
dataMap.put("id","a");
dataMap.put("name","John");
dataMap.put("address","Star City");
dataMap.put("department","Medical");
DumperOptions options = new DumperOptions();
options.setIndent(2);
options.setPrettyFlow(true);
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
Yaml yaml = new Yaml(options);
PrintWriter writer = null;
try {
writer = new PrintWriter(new File("./src/main/resources/admins.yml"));
yaml.dump(dataMap,writer);
} catch (FileNotFoundException e) {
e.printstacktrace();
}
解决方法
如果你想要一个嵌套的 YAML 结构,创建一个嵌套的数据结构:
dataMap.put("admins",Map.of("test",Map.of("id",1234)));