Java 使用hashMap、YAML序列化缺少输出
嗨,我正在学习如何使用snakeYAML 我想保存一个库对象,以便在启动应用程序时再次加载它。 我只是想把我的图书馆和书放在一起。互联网告诉我yaml是一个很好的方法 我必须上以下课程:Java 使用hashMap、YAML序列化缺少输出,java,serialization,hashmap,yaml,snakeyaml,Java,Serialization,Hashmap,Yaml,Snakeyaml,嗨,我正在学习如何使用snakeYAML 我想保存一个库对象,以便在启动应用程序时再次加载它。 我只是想把我的图书馆和书放在一起。互联网告诉我yaml是一个很好的方法 我必须上以下课程: public class Library { private HashMap<String, List<Book>> library; public Library() { library = new HashMap<String, List<Book>&g
public class Library {
private HashMap<String, List<Book>> library;
public Library() {
library = new HashMap<String, List<Book>>();
}
//getter
public HashMap<String, List<Book>> getHashMap() {
return library;
}
//setter
public void setHashMap(HashMap<String, List<Book>> library) {
this.library = library;
}
}
有些事告诉我我错过了像图书馆本身这样的东西。那一定是因为你只是
dump()
列出了两本书,当然不是整个图书馆!这么简单的错误!有了这些,我就结束了!谢谢
public static void main(String[] args) {
Library library = new Library();
LinkedList<Book> books = new LinkedList<Book>();
books.add(new Book("Some title", false));
books.add(new Book("Other Title", true));
library.putMany("books", books);
System.out.println(new Yaml().dump(books));
- !!model.Book {done: false, title: Some title}
- !!model.Book {done: true, title: Other Title}