0

I'm trying to convert an object, and this object has an ArrayList object has attribute and an integer.

But when i use to toString() on the JSON object I got pointers instead of the object:

{"class":"class model.ListResult","listVideo":["model.Video@706a4d1a","model.Video@52ec1f9e","model.Video@c0fe89a","model.Video@686fdca5","model.Video@7ff0a34","model.Video@78f6e005","model.Video@17eda64e","model.Video@73415727","model.Video@46c0fc8e"],"numberOfResult":109}

Here is the code for my class:

public class ListResult implements Serializable {

    private int numberOfResult;
    private ArrayList<Video> listVideo;

    /**
     * Constructor
     * 
     * @param allVideoNumber
     * @param listVideo2
     */
    public ListResult(int numberOfResult, ArrayList<Video> listVideo) {

        this.numberOfResult = numberOfResult;
        this.listVideo = listVideo;

    }

    public int getNumberOfResult() {
        return numberOfResult;
    }
    public void setNumberOfResult(int numberOfResult) {
        this.numberOfResult = numberOfResult;
    }
    public ArrayList<Video> getListVideo() {
        return listVideo;
    }
    public void setListVideo(ArrayList<Video> listVideo) {
        this.listVideo = listVideo;
    }

}

And my method call :

public String getListJson(boolean escape, int from, int number, String order, String by, ArrayList<Category> listCategory) throws Exception {

    //get list of video
    ArrayList<Video> listVideo = dbVideo.getAll(from, number, order, by, listCategory);

    JSONObject jsObject = new JSONObject(new ListResult(dbVideo.getAllVideoNumber(listCategory), listVideo));

    if(escape) {
        String  r;

        r = jsObject.toString();
        r = r.replace("\\", "\\\\");
        r = r.replace("'", "\\'");

        return r;

    }

    return jsObject.toString();

}
Jerome Ansia
  • 6,854
  • 11
  • 53
  • 99

2 Answers2

2

It's probably because the toString() method calls all the other objects' toString() method and those are not overridden. That gives the effect that they will print their name (the class name) and the "address" that they are located at in the JVM. One way to solve this would be to override said method and give it some better implementation. Although i don't know if this is a good idea and perhaps you should have a look at serialization instead if you want to do this a lot. But for this simple case i think a simple override of the toString() method should suffice if it doesn't break some other functionality. What i mean is something along these lines:

class Video {
    public String toString() {
        return this.title + " " + this.yearOfRelease;
    }
}

Where you perhaps need to edit this to be the proper JSON output.

Although i should add that im not certain about this and just guessing how the toString() method for JSONObject works. It could be something entirely different. But it's worth a shot!

Edit: After looking a little at the JSONObject docs: http://www.json.org/javadoc/org/json/JSONObject.html It would seem that you should use the put() method to insert your elements, and then use the toString() method. Ie, you would need to loop over all elements in the list and add them one by one. Or the override of toString() in your Video class might do the work.

Edit2: Furthermore it would seem that http://www.json.org/javadoc/org/json/JSONObject.html#put%28java.lang.String,%20java.util.Collection%29 is of interest to you, it will take a Collection which your list should be, that could perhaps solve things.

lfxgroove
  • 3,778
  • 2
  • 23
  • 33
  • i should overwrite the toString() of my class ListResult with a JSonObject(this).toString(); ? Edit: yes the thing is that class contains a lot of attributes so if there is another easier solution that will be nice :) Thanks ;) – Jerome Ansia Nov 27 '12 at 19:53
  • 1
    OK i thing i got it with you last edit :) i should change the ArrayList to a JSONArray that should make the trick i guess, let me a few minutes to try this ;) – Jerome Ansia Nov 27 '12 at 20:00
  • 1
    Hope it works out! Gonna go to bed now. – lfxgroove Nov 27 '12 at 20:03
0

Old post, but anyway here's what solved my problem. You must have getters (and setters?) for all of your attributes of Video object!

Pajl
  • 1