新的项目中需要将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);
}
其具体步骤是这样:
(1)使用Yaml类,创建一个Yaml对象,所有的解析操作都是从这个对象开始;
(2)声明了一个yaml的字符串(当然也可以使用yaml文档等),定义了一个对象:key: hello yaml;
(3)使用Yaml对象的load方法加载一段yaml字符串,返回解析之后的对象,其实这个对象是一个Map:LinkedHashMap;
三、解析为对象
假设有如下yml配置文件需要解析:
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
定义javaBean如下:
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使用的更多相关文章

  1. Java使用snakeyaml解析yaml

    YAML Yaml是一种"是一个可读性高并且容易被人类阅读,容易和脚本语言交互,用来表达资料序列的编程语言."类似于XML但比XML更简洁,语法详见 http://www.ruan ...

  2. snakeyaml - Documentation.wiki

    SnakeYAML Documentation This documentation is very brief and incomplete. Feel free to fix or improve ...

  3. 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 ...

  4. 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 ...

  5. SpringCloud报错:Caused by: org.yaml.snakeyaml.parser.ParserException: while parsing MappingNode

    今天在配置eureka集群时,SpringCloud报错如下: Caused by: org.yaml.snakeyaml.parser.ParserException: while parsing ...

  6. 错误: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 ...

  7. org.yaml.snakeyaml.parser.ParserException: while parsing a block mapping

    org.yaml.snakeyaml.parser.ParserException: while parsing a block mapping 原因:yml文件格式错误,此文件要求严格要求格式 如节 ...

  8. JAVA使用SnakeYAML解析与序列化YAML

    1.概述 本文,我们将学习如何使用SnakeYAML库将 YAML文档转换为Java对象,以及JAVA对象如何序列化为YAML文档. 2.项目设置 要在项目中使用SnakeYAML,需要添加Maven ...

  9. org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next tok

    org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next tokenfound character ‘@’ th ...

随机推荐

  1. oracle如何保证读一致性 第二弹

    Oracle之数据库一致性读的原理 在Oracle数据库中,undo主要有三大作用:提供一致性读(Consistent Read).回滚事务(Rollback Transaction)以及实例恢复(I ...

  2. HUD:2853-Assignment(KM算法+hash)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2853 Assignment Time Limit: 2000/1000 MS (Java/Others) ...

  3. 【转载】美国人教你这样用Google

    大前提:英文Google→www.google.com 第一篇 在搜索框上输入:“indexof/”inurl:lib 再按搜索你将进入许多图书馆,并且一定能下载自己喜欢的书籍. 在搜索框上输入:“i ...

  4. MongoDB学习-->Gridfs分布式存储&DBRef关联查询

    mongodb自带的一个分布式文件系统 fs.files _id filename md5 size uploaddate contenttype metadata {"user_id&qu ...

  5. loj2021 「HNOI2017」大佬

    there #include <algorithm> #include <iostream> #include <cstring> #include <cst ...

  6. 封装BackgroundWorker控件(提供源代码下载,F5即可见效果)

    Demo源码 背景 经常做些小程序或者小DEMO的时候会用到异步,多线程来执行一些比较耗时的工作同时将进度及时进行反馈.我通常会使用位于[ System.ComponentModel]命名空间下的Ba ...

  7. selenium非常好的资料收集

    非常全的中文资料:http://qi-ling2006.iteye.com/ http://blog.csdn.net/qq744746842/article/details/49926917

  8. 2017"百度之星"程序设计大赛 - 资格赛

    度度熊与邪恶大魔王  Accepts: 3666  Submissions: 22474  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: ...

  9. VirtualBox Host-only Adapter,Failed to create the host-only adapter 转

    不用重装VirtualBox,安装虚拟网卡 今天使用VirtualBox的host-only模式,因为之前把网络连接卸载,这次出现的各种错误. Failed to create the host-on ...

  10. 广东工业大学2016校赛决赛重现——E积木积水(方法据说很多)

    Problem E: 积木积水 Description 现有一堆边长为1的已经放置好的积木,小明(对的,你没看错,的确是陪伴我们成长的那个小明)想知道当下雨天来时会有多少积水.小明又是如此地喜欢二次元 ...