JSON到Java Mapping帮助 - 嵌套集合

时间:2011-03-11 10:42:39

标签: java json

我正试图想出一种方法将这个JSON String转换为Java对象图,但我无法这样做。下面,我插入了我的JSON字符串和我的两个类。我已经验证它是一个有效的json结构。我一直在尝试googles api(http://sites.google.com/site/gson/gson-user-guide),但它没有映射嵌套的Photo Collection。任何想法或替代图书馆?

{"photos":{"page":1,"pages":73514,"perpage":50,"total":"3675674","photo":[{"id":"5516612975","owner":"23723942@N07","secret":"b8fb1fda57","server":"5213","farm":6,"title":"P3100006.JPG","ispublic":1,"isfriend":0,"isfamily":0},{"id":"5516449299","owner":"81031835@N00","secret":"67b56722da","server":"5171","farm":6,"title":"Kaiser Boys Volleyball","ispublic":1,"isfriend":0,"isfamily":0}]},"stat":"ok"}

Photos.java

public class Photos {
private int pages;
private int perpage;
private String total;
private List<Photo> photo;
private String stat;
public int getPages() {
    return pages;
}
public void setPages(int pages) {
    this.pages = pages;
}
public int getPerpage() {
    return perpage;
}
public void setPerpage(int perpage) {
    this.perpage = perpage;
}
public String getTotal() {
    return total;
}
public void setTotal(String total) {
    this.total = total;
}
public List<Photo> getPhoto() {
    return photo;
}
public void setPhoto(List<Photo> photo) {
    this.photo = photo;
}
public String getStat() {
    return stat;
}
public void setStat(String stat) {
    this.stat = stat;
}

}

Photo.java:

public class Photo {
private String id;
private String owner;
private String secret;
private String server;
private String farm;
private String title;
private int isPublic;
private int isFriend;
private int isFamily;
public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getOwner() {
    return owner;
}
public void setOwner(String owner) {
    this.owner = owner;
}
public String getSecret() {
    return secret;
}
public void setSecret(String secret) {
    this.secret = secret;
}
public String getServer() {
    return server;
}
public void setServer(String server) {
    this.server = server;
}
public String getFarm() {
    return farm;
}
public void setFarm(String farm) {
    this.farm = farm;
}
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}
public int getIsPublic() {
    return isPublic;
}
public void setIsPublic(int isPublic) {
    this.isPublic = isPublic;
}
public int getIsFriend() {
    return isFriend;
}
public void setIsFriend(int isFriend) {
    this.isFriend = isFriend;
}
public int getIsFamily() {
    return isFamily;
}
public void setIsFamily(int isFamily) {
    this.isFamily = isFamily;
}   

}

1 个答案:

答案 0 :(得分:0)

使用Jackson。它将负责转换到JSON和从JSON转换。它提供了多种接近转换的方法,并与Spring等框架很好地集成。绝对是一个知道的人。