1、yaml文件注重格式,如空格等
2、本次实现数组数据读取:
注意username和手机号之间的空格,不然识别出来的是string而不是键值对
解析代码:
InputStream in = ClassLoader.getSystemResourceAsStream("yaml/login.yaml");
Yaml yaml = new Yaml();
Map<String, Object> map = yaml.loadAs(in, Map.class);
List<Map<String,String> > appid = (List<Map<String,String>>) 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<Map<String,Object> > vv = (List<Map<String, Object>>) result.get("user1");
\\ System.out.println(vv.size()+" "+vv.get(0).get("password"));
3、yamlbean读取
<dependency>
<groupId>com.github.sanjusoftware</groupId>
<artifactId>yamlbeans</artifactId>
<version>1.11</version>
</dependency>
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<Stu> 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()+" ");
}
网友评论