SnakeYaml 转储函数用单引号写入 [英] SnakeYaml dump function writes with single quotes

查看:42
本文介绍了SnakeYaml 转储函数用单引号写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

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'

但我想要没有单引号的输出

But i want the output without the single quotes like

NAME1: Raj
NAME2: Kumar

这东西解析文件很舒服.如果有人有解决方案,请帮我解决.提前致谢

This thing is very comfortable for parsing the file. If anyone have solution, please help me to fix. Thanks in advance

推荐答案

好吧,SnakeYaml 完全按照您的要求执行:对于 Map 中的每个条目,它转储键的串联,字符串 ": ",并将值作为 YAML 文档.字符串映射到 YAML 中的标量,并且由于标量包含一个 : 后跟空格,所以它必须被引用(否则它将是一个键值对).

Well SnakeYaml does exactly what you tell it to: For each entry in the Map, it dumps the concatenation of the key, the String ": ", and the value as YAML document. A String maps to a Scalar in YAML, and since the scalar contains a : followed by space, it must be quoted (else it would be a key-value pair).

实际上想要做的是将 Map 转储为 YAML 映射.你可以这样做:

What you actually want to do is to dump the Map as YAML mapping. You can do it like this:

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);
}

这篇关于SnakeYaml 转储函数用单引号写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆