Solved

JSON to Java Mapping Help - Nested Collection

Posted on 2011-03-11
2
800 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 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

Automating Your MSP Business

The road to profitability.
Delivering superior services is key to ensuring customer satisfaction and the consequent long-term relationships that enable MSPs to lock in predictable, recurring revenue. What's the best way to deliver superior service? One word: automation.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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.

615 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