数据驱动之yaml读取

1、yaml文件注重格式,如空格等
2、本次实现数组数据读取:


yaml的内容如下: 数据驱动之yaml读取
文章图片
image.png 数据驱动之yaml读取
文章图片
image.png 【数据驱动之yaml读取】注意username和手机号之间的空格,不然识别出来的是string而不是键值对
解析代码:

InputStream in = ClassLoader.getSystemResourceAsStream("yaml/login.yaml"); Yaml yaml = new Yaml(); Map map = yaml.loadAs(in, Map.class); List appid = (List) map.get("user"); System.out.println(appid.get(1).get("username")); \\ FileReader fileReader=new FileReader("E:\\ideawork\\axxqa-st\\src\\main\\resources\\yml\\data.yml"); \\ Yaml yamlc = new Yaml(); \\ File f = new File("test.yaml"); \\ Map result= (Map) yamlc.load(fileReader); \\ List vv = (List) result.get("user1"); \\ System.out.println(vv.size()+""+vv.get(0).get("password"));

3、yamlbean读取
com.github.sanjusoftware yamlbeans 1.11

yaml文件:
user: - username: "13222283" password: "204011"

yaml文件对应的bean类
import lombok.Getter; import lombok.Setter; import java.util.List; @Getter @Setter public class B { private List user; @Getter @Setter public static class Stu{ private String username; private String password; } }

测试类的具体实现
YamlReader reader = new YamlReader(new FileReader("E:\\ideawork\\axxqa-axxol-apitest\\src\\main\\resources\\yml\\data.yml")); C contact = null; try { contact = reader.read(C.class); } catch (YamlException e) { e.printStackTrace(); } for(C.User student:contact.getUser()){ System.out.println("student.getId()=" + student.getUsername()+""); }

    推荐阅读