有一个模型类:
public class Book {
private String id;
private Set<String> authors = new HashSet<String>(); //Set field
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Set<String> getAuthors() {
return authors;
}
public void setAuthors(Set<String> authors) {
this.authors = authors;
}
@Override
public String toString() {
return "Book [authors=" + authors + ", id=" + id + "]";
}
}
我执行下面的函数:
public void test3() {
Book book = new Book();
book.setId("T2");
Set<String> authors = new HashSet<String>();
authors.add("Tom");
authors.add("King");
book.setAuthors(authors );
JSONObject jsonObject = JSONObject.fromObject(book);
System.out.println(jsonObject);
Book b = (Book) JSONObject.toBean(jsonObject, Book.class);
System.out.println(b.toString());
}
无法将 jsonObject 转换为 Book 对象,会出现异常。如何将 jsonObject 成功转换为 Book 对象?