0

我正在尝试转换一个对象,并且该对象具有一个 ArrayList 对象,该对象具有属性和一个整数。

但是当我在 JSON 对象上使用 toString() 时,我得到的是指针而不是对象:

{"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}

这是我的课程的代码:

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

}

我的方法调用:

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();

}
4

2 回答 2

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.

于 2012-11-27T19:52:21.967 回答
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!

于 2013-12-13T16:07:34.810 回答