Link to home
Start Free TrialLog in
Avatar of cgray1223
cgray1223

asked on

JSON to Java Mapping Help - Nested Collection

Hello,

I'm trying to figure out a way to transform this JSON String into a Java object graph but I'm unable to do so. Below, I've inserted my JSON String, and my two classes. I've verified that its a valid json structure. I've been trying googles api (http://sites.google.com/site/gson/gson-user-guide) but it doesn't map the nested Photo Collection. Any ideas or alternate libraries?
{"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"}

Open in new window


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;
}
}

Open in new window


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;
}   
}

Open in new window


ASKER CERTIFIED SOLUTION
Avatar of momi_sabag
momi_sabag
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cgray1223
cgray1223

ASKER

I ended up using Jackson and a wrapper class around Photos called FlickrResponse that has a single instance of Photos.  Photos has a List<Photo> and then Jackson mapped the response from Flickr.

  ObjectMapper mapper = new ObjectMapper();
   FlickrResponse flickrResponse = mapper.readValue(finalStr, FlickrResponse.class);