Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JSON to Java Mapping Help - Nested Collection

Posted on 2011-03-11
2
Medium Priority
?
803 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 37

Accepted Solution

by:
momi_sabag earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These instructions are based on installing Owncloud on your new raspberry pi connected with a usb HDD. What do you need Part A? A Raspberry Pi, model B. A boot SD card for the Raspberry Pi. A usb HDD An Ethernet cable to connect to the lo…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.
Suggested Courses

704 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