SnakeYaml dump 自动转换数字(表示为字符串)
Posted
技术标签:
【中文标题】SnakeYaml dump 自动转换数字(表示为字符串)【英文标题】:SnakeYaml dump automatically converts numbers(represented as Strings) 【发布时间】:2017-12-31 05:50:26 【问题描述】:我正在使用 yaml 转储方法为我的对象创建配置文件。我有以下代码:
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
我希望显示的电话号码不带单引号。我注意到使用带有对象类型的映射来解决这个问题,但我需要值类型是字符串。我该如何解决这个问题?
【问题讨论】:
你可以使用String类的replaceAll方法将所有的'替换为emply空格,比如replaceAll("'",""); 【参考方案1】: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
【讨论】:
以上是关于SnakeYaml dump 自动转换数字(表示为字符串)的主要内容,如果未能解决你的问题,请参考以下文章