SnakeYaml使用
新的项目中需要将yaml文件解析为对象,调研了决定使用snakeYaml,下面看一看怎么使用。
一、引入依赖
因为项目是使用maven构建的,所以我们在pom文件中引入snakeYaml的依赖,如下:
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.21</version>
</dependency>
二,简单示例
public void testLoad() {
String yamlStr = "key: hello yaml";
Yaml yaml = new Yaml();
Object ret = yaml.load(yamlStr);
System.out.println(ret);
}
(2)声明了一个yaml的字符串(当然也可以使用yaml文档等),定义了一个对象:key: hello yaml;
(3)使用Yaml对象的load方法加载一段yaml字符串,返回解析之后的对象,其实这个对象是一个Map:LinkedHashMap;
given : Chris
family : Dumars
address:
-
lines: 458 Walkman
city : Royal Oak
state : MI
postal : 48046
-
lines: 459 Walkman
city : Royal Oak
state : MI
postal : 48046
public class Person {
private String given;
private String family;
private List<Address> address;
//省略getter和setter方法
}
public class Address {
private String lines;
private String city;
private String state;
private Integer postal;
//省略getter和setter方法
}
public void parsePerson() {
//指定yaml文件的root对象解析成Person类型
Yaml yaml = new Yaml(new Constructor(Person.class));
Person ret = (Person) yaml.load(this.getClass().getClassLoader()
.getResourceAsStream("person.yaml"));
Assert.assertNotNull(ret);
Assert.assertEquals("MI", ret.getAddress().get(0).getState());
}
SnakeYaml使用的更多相关文章
- Java使用snakeyaml解析yaml
YAML Yaml是一种"是一个可读性高并且容易被人类阅读,容易和脚本语言交互,用来表达资料序列的编程语言."类似于XML但比XML更简洁,语法详见 http://www.ruan ...
- snakeyaml - Documentation.wiki
SnakeYAML Documentation This documentation is very brief and incomplete. Feel free to fix or improve ...
- org.yaml.snakeyaml.error.YAMLException: java.nio.charset.MalformedInputException: Input length = 1
项目启动报错2018-12-21 14:06:24.917 INFO 23472 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refr ...
- VS Code打开使用IDEA搭建的Spring Boot项目运行提示"snakeyaml was not found on the classpath"错误
今天用VS Code打开之前基于IDEA搭建并开发的Spring Boot项目,启动调试后出现如下错误: 17:43:05.214 [restartedMain] ERROR org.springfr ...
- SpringCloud报错:Caused by: org.yaml.snakeyaml.parser.ParserException: while parsing MappingNode
今天在配置eureka集群时,SpringCloud报错如下: Caused by: org.yaml.snakeyaml.parser.ParserException: while parsing ...
- 错误:Attempted to load applicationConfig: [classpath:/application.yml] but snakeyaml was not found on the classpath
MyEclipse导入工程,报错如下: ::42.187 [main] ERROR org.springframework.boot.SpringApplication - Application r ...
- org.yaml.snakeyaml.parser.ParserException: while parsing a block mapping
org.yaml.snakeyaml.parser.ParserException: while parsing a block mapping 原因:yml文件格式错误,此文件要求严格要求格式 如节 ...
- JAVA使用SnakeYAML解析与序列化YAML
1.概述 本文,我们将学习如何使用SnakeYAML库将 YAML文档转换为Java对象,以及JAVA对象如何序列化为YAML文档. 2.项目设置 要在项目中使用SnakeYAML,需要添加Maven ...
- org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next tok
org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next tokenfound character ‘@’ th ...
随机推荐
- LeetCode(228) Summary Ranges
题目 Given a sorted integer array without duplicates, return the summary of its ranges. For example, g ...
- Linux 关于SELinux的命令及使用
1. 模式的设置 : 修改/etc/selinux/config文件中的SELINUX=”" 的值 ,然后重启.enforcing:强制模式,只要selinux不允许,就无法执行 permi ...
- POJ:2411-Mondriaan's Dream(矩形拼接方案)
题目链接:http://poj.org/problem?id=2411 解题心得: 可以说是很经典的一个状压dp了,写dfs遍历肯定是要超时的,这个题的状态转移方程对新手来说有点吃力. 状态转移用的是 ...
- pandas修改列名
- IDEA-常用插件,使用FindBugs寻找bug,代码分析
bug无处不在,但是我们总希望少一点bug. 最近发现了一款好用的寻找bug的插件,特此记下. 一.安装 路径:File-->Settings-->Plugins-->Browse ...
- luogu1251 餐巾计划问题
ss是源点,代表餐巾卖家,tt是汇点,代表记账收钱者. 记p(i)是i天早晨的可用毛巾数,q(i)是i天完了的废毛巾数. 建图见注释 #include <iostream> #includ ...
- Ext.js给form加背景图片
{ iconCls: 'zyl_icons_showdetail', tooltip: '查看', handler: function(gridView, rowIndex, colIndex) { ...
- 缓存淘汰算法之LFU
1. LFU类 1.1. LFU 1.1.1. 原理 LFU(Least Frequently Used)算法根据数据的历史访问频率来淘汰数据,其核心思想是“如果数据过去被访问多次,那么将来被访问的频 ...
- JavaScript 专题系列第六篇,讲解深浅拷贝的技巧和以及实现深浅拷贝的思路
拷贝也是面试经典呐! 数组的浅拷贝 如果是数组,我们可以利用数组的一些方法比如:slice.concat 返回一个新数组的特性来实现拷贝. 比如: var arr = ['old', 1, tru ...
- Stringsobits(模拟)
描述 Consider an ordered set S of strings of N (1 <= N <= 31) bits. Bits, of course, are either ...