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 ...
随机推荐
- w3resource_MySQL练习: Aggregate_functions
w3resource_MySQL练习题:Aggregate_functions 1. Write a query to list the number of jobs available in t ...
- POJ:1904-King's Quest
King's Quest Time limit15000 ms Case time limit2000 ms Memory limit65536 kB Description Once upon a ...
- POJ:1086-Parencodings
Parencodings Time Limit: 1000MS Memory Limit: 10000K Description Let S = s1 s2-s2n be a well-formed ...
- python字符串、列表和字典的说明
python字符串.列表和字典的说明 字符串.列表.字典 字符串的作用存储一段数据信息.例如 info = '我爱北京天安门' ,在调取的时候可以直接调取,灵活方便,print(info) 就可以把刚 ...
- 前端 五——ajax
内容概要: 1.ajax的特点 2.基于JS的ajax 3.基于jQuery的ajax 1.特点: 局部刷新 异步传送(交互) 缺点: (1)无形中向服务器发送的请求次数太多,导致服务器压力增大. ( ...
- webdriver高级应用- 修改Chrome设置伪装成手机M站
通过更改PC端Chrome浏览器的属性值,将PC端Chrome浏览器设定为手机端尺寸的浏览器,以便模拟手机端的浏览器,并完成各种页面操作. #encoding=utf-8from selenium i ...
- Selenium WebDriver- 操作浏览器的cookie
#encoding=utf-8 import unittest import time from selenium import webdriver from selenium.webdriver i ...
- python+selenium面试题
selenium中如何判断元素是否存在? selenium中没有提供原生的方法判断元素是否存在,一般我们可以通过定位元素+异常捕获的方式判断. # 判断元素是否存在 try: dr.find_elem ...
- TensorFlow——小练习:counter
下面的例子演示了如何使用变量实现一个 简单的计数器(counter) # _*_coding:utf-8_*_ import tensorflow as tf import numpy as np # ...
- Linux硬件资源管理与外设设备使用、系统运行机制及用户管理
Linux硬件资源管理 PCI设备 显卡 $>>dmesg |grep -i vga[ 0.000000] Console: colour VG ...