SnakeYaml自动将数字(表示为字符串)转换为Dump格式

3
我正在使用YAML dump方法为我的对象创建配置文件。我有以下代码:
Map<String, String> map = new HashMap<>();
map.put("phonenumber","7285042388");
map.put("name","Cristina");
DumperOptions dumperOptions1 = new DumperOptions();
dumperOptions1.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
Yaml yaml2 = new Yaml(dumperOptions);
System.out.println(yaml2.dump(map));`

输出:

phonenumber: '7285042388'

name: Cristina

我希望电话号码不要显示单引号。我注意到使用值为Object类型的映射可以解决这个问题,但我需要值的类型是String。我该如何解决这个问题?


你可以使用String类的replaceAll方法将所有的单引号替换为空格,例如:replaceAll("'", ""); - SpringLearner
1个回答

0
Map<String, Object> map = new HashMap<>();
map.put("phonenumber",  new BigInteger("7285042388"));
map.put("name","Cristina");
DumperOptions dumperOptions1 = new DumperOptions();
dumperOptions1.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
Yaml yaml2 = new Yaml(dumperOptions1);
System.out.println(yaml2.dump(map));

输出:

phonenumber: 7285042388
name: Cristina

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接