写一段yaml文件,并且给出对应的java实体类和解析类
时间: 2023-07-16 14:13:45 浏览: 156
yaml文件解析(java)
以下是一个示例的YAML文件:
```
# example.yaml
person:
name: John
age: 30
address:
street: Elm Street
city: Anytown
state: CA
zip: 12345
```
对应的Java实体类:
```java
public class Person {
private String name;
private int age;
private Address address;
// getters and setters
}
public class Address {
private String street;
private String city;
private String state;
private int zip;
// getters and setters
}
```
对应的解析类:
```java
import org.yaml.snakeyaml.Yaml;
import java.io.InputStream;
public class YAMLParser {
public static void main(String[] args) {
Yaml yaml = new Yaml();
InputStream inputStream = YAMLParser.class
.getClassLoader()
.getResourceAsStream("example.yaml");
Person person = yaml.loadAs(inputStream, Person.class);
System.out.println(person.getName());
}
}
```
该解析类使用了`snakeyaml`库来读取YAML文件并将其转换为Java对象。在这个例子中,我们将`example.yaml`文件解析成了`Person`对象,并打印了人名。
阅读全文