首先引入snakeyaml-1.16.jar的包.
直接上代码:
package com.ming.yaml;import java.util.Map;import org.yaml.snakeyaml.Yaml;public class Test { public static void main(String[] args) { //yaml转json Yaml yaml = new Yaml(); String document = " a: 1\n b:\n c: 3\n d: 4\n"; System.out.println(document); System.out.println(yaml.dump(yaml.load(document))); //yaml Map取值 Yaml yaml1 = new Yaml(); String document1 = "hello: 25"; Map map = (Map) yaml.load(document1); System.out.println("{hello=25}"+ map.toString()); System.out.println(""+new Long(25)+map.get("hello")); }}
运行效果如下:
先笔记一下,也许有用.