<dependency> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> <version>1.27</version> </dependency> import org.yaml.snakeyaml.Yaml; Yaml yaml = new Yaml(); yaml person: name: John Doe age: 30 address: city: New York country: USA import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Map; Map<String, Object> data; try { data = yaml.load(new FileInputStream(new File("data.yml"))); Map<String, Object> person = (Map<String, Object>) data.get("person"); String name = (String) person.get("name"); int age = (int) person.get("age"); Map<String, Object> address = (Map<String, Object>) person.get("address"); String city = (String) address.get("city"); String country = (String) address.get("country"); } catch (FileNotFoundException e) { e.printStackTrace(); } public class Person { private String name; private int age; private Address address; } Person person = new Person(); person.setName("John Doe"); person.setAge(30); person.setAddress(new Address("New York", "USA")); String yamlString = yaml.dump(person); System.out.println(yamlString); yaml !!com.example.Person name: John Doe age: 30 address: !!com.example.Address city: New York country: USA


上一篇:
下一篇:
切换中文