Since I came across this post to read a list of User objects, here's what I did with Jackson
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class LoadContacts {
private static String yamlLocation = "path_to/../contacts.yml";
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
try {
List<Contact> contactList = mapper.readValue(new File(yamlLocation), new TypeReference<List<Contact>>(){});
contactList.forEach(System.out::println);
} catch (Exception e) {
e.printStackTrace();
}
}
}
mapper.readValue(..)accepts several arguments, such as URL, String for the first parameter. This solution uses File. One change I made for the OP to work correctly was to determine the phone number as follows:
public List<Phone> phoneNumbers;
source
share