<dependency>
<groupId>net.jcazevedo</groupId>
<artifactId>scala-yaml_2.11</artifactId>
<version>0.6.0</version>
</dependency>
groovy
implementation 'net.jcazevedo:scala-yaml_2.11:0.6.0'
yaml
name: John Doe
age: 30
address:
street: 123 Main St
city: Anytown
country: USA
import net.jcazevedo.moultingyaml.*;
import java.io.File;
public class Main {
public static void main(String[] args) {
File yamlFile = new File("example.yaml");
YamlObject yamlObject = YamlLoader.load(yamlFile);
YamlObject rootNode = (YamlObject) yamlObject;
String name = rootNode.fields().get(YamlString("name")).get().convertTo[String];
int age = rootNode.fields().get(YamlString("age")).get().convertTo[Int];
YamlObject addressNode = (YamlObject) rootNode.fields().get(YamlString("address")).get();
String street = addressNode.fields().get(YamlString("street")).get().convertTo[String];
String city = addressNode.fields().get(YamlString("city")).get().convertTo[String];
String country = addressNode.fields().get(YamlString("country")).get().convertTo[String];
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Address: " + street + ", " + city + ", " + country);
}
}
Name: John Doe
Age: 30
Address: 123 Main St, Anytown, USA