import org.yaml.snakeyaml.Yaml; public class SnakeYAMLExample { public static void main(String[] args) { Yaml yaml = new Yaml(); String yamlString = "--- name: John age: 25"; Map<String, Object> data = yaml.load(yamlString); String name = (String) data.get("name"); int age = (int) data.get("age"); System.out.println("Name: " + name); System.out.println("Age: " + age); } } import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import java.io.File; import java.io.IOException; public class JacksonYAMLExample { public static void main(String[] args) throws IOException { ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()); File yamlFile = new File("example.yml"); MyObject myObject = objectMapper.readValue(yamlFile, MyObject.class); objectMapper.writeValue(new File("output.yml"), myObject); } } class MyObject { private String name; private int age; } yaml server: port: 8080 context-path: /myapp spring: datasource: url: jdbc:mysql://localhost:3306/mydb username: root password: mypassword


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