1 回答
TA贡献1827条经验 获得超7个赞
为了获得正确的样式,您需要创建并配置一个DumperOptions
对象并将其传递给类的构造函数Yaml
。
public static void main(String[] args) {
Map<String, Object> root = new LinkedHashMap<>();
Map<String, Object> mart = new LinkedHashMap<>();
Map<String, Object> details = new LinkedHashMap<>();
details.put("name", "Koushik");
details.put("purpose", "yaml generation for testing");
details.put("owner", Arrays.asList(
Map.of("name", "Bobby", "email", "bd@abc.com"),
Map.of("name", "Chaminda", "email", "cv@def.com")
));
mart.put("details", details);
root.put("mart", mart);
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
Yaml yaml = new Yaml(options);
System.out.println(yaml.dump(root));
}
输出:
mart:
details:
name: Koushik
purpose: yaml generation for testing
owner:
- name: Bobby
email: bd@abc.com
- name: Chaminda
email: cv@def.com
另外,您可以创建Representer
s和Resolver
s,以便可以使用自定义类而不是嵌套的Map
。
添加回答
举报