Solved

JSON to Java Mapping Help - Nested Collection

Posted on 2011-03-11
2
790 Views
Last Modified: 2012-08-13
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


0
Comment
Question by:cgray1223
2 Comments
 
LVL 37

Accepted Solution

by:
momi_sabag earned 500 total points
ID: 35113375
i had a similar problem and this what i did:
i created an instance of my objects and placed data in it
than used gson to convert it to json, just to make sure i had my json string in the correct format

than i tried creating an instance of my object and that to the gson.fromJson method, which taught me that i had to use LinkedList instead of ArrayList

after you will do all that, if it still won't work, you should be able to write your own serializer and deserializer
0
 

Author Comment

by:cgray1223
ID: 35114611
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);
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now