1 回答

TA貢獻1827條經驗 獲得超8個贊
為了獲得正確的樣式,您需要創建并配置一個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", "[email protected]"),
? ? ? ? ? ? Map.of("name", "Chaminda", "email", "[email protected]")
? ? ));
? ? 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: [email protected]
? ? - name: Chaminda
? ? ? email: [email protected]
另外,您可以創建Representer
s和Resolver
s,以便可以使用自定義類而不是嵌套的Map
。
添加回答
舉報