考虑以下代码:
public void testDumpWriter() {
Map<String, Object> data = new HashMap<String, Object>();
data.put("NAME1", "Raj");
data.put("NAME2", "Kumar");
Yaml yaml = new Yaml();
FileWriter writer = new FileWriter("/path/to/file.yaml");
for (Map.Entry m : data.entrySet()) {
String temp = new StringBuilder().append(m.getKey()).append(": ").append(m.getValue()).toString();
yaml.dump(temp, file);
}
}
上述代码的输出是
'NAME1: Raj'
'NAME2: Kumar'
但我想要不带单引号的输出,例如
NAME1: Raj
NAME2: Kumar
这个东西对于解析文件来说非常舒服。
如果有人有解决方案,请帮我解决。提前致谢
SnakeYaml 完全按照您的指示执行:对于 Map 中的每个条目,它转储键、字符串的串联": "
,以及 YAML 文档形式的值。在 YAML 中,字符串映射到标量,并且由于标量包含:
后面跟空格,必须用引号引起来(否则它将是一个键值对)。
你什么actually想要做的是将 Map 转储为 YAML 映射。你可以这样做:
public void testDumpWriter() {
Map<String, Object> data = new HashMap<String, Object>();
data.put("NAME1", "Raj");
data.put("NAME2", "Kumar");
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
Yaml yaml = new Yaml(options);
FileWriter writer = new FileWriter("/path/to/file.yaml");
yaml.dump(data, writer);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)