-1

i am new to web services and i am using gson library for the first time . I am able to parse certain simple json string but i am stuck here . This is my response

{"message":"Action completed successfully.","results":3,"action":"param","data":[{"title":"test1","id":2,"completeCount":0},{"title":"test2","id":3,"completeCount":0},{"title":"test2","id":3,"completeCount":0}],"success":true}

This is how i am trying to parse it

Gson gson = new Gson();
Chracter character = gson.fromJson(successResponse, Character.class);

And this is my class

public class Character {


private  List<Detail> data = new ArrayList<Detail>();
private String action ;
private  int results;
private String message;
private Detail detail;


public Character() {

}

public Character(List<Detail> data, String action, int results,
        String message) {
    super();
    this.data = data;
    this.action = action;
    this.results = results;
    this.message = message;
}

public List<Detail> getData() {
    return data;
}

public void setData(List<Detail> data) {
    this.data = data;
}

public String getAction() {
    return action;
}

public void setAction(String action) {
    this.action = action;
}

public int getResults() {
    return results;
}

public void setResults(int results) {
    this.results = results;
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}


public Detail getDetail() {
    return detail;
}

public void setDetail(Detail detail) {
    this.detail = detail;
}

public class Detail{

    private String title;
    private int id;
    private int completeCount ;


    public Detail() {

    }

    public Detail(String title, int id, int completeCount) {
        super();
        this.title = title;
        this.id = id;
        this.completeCount = completeCount;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getId() {
        return Id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getCompleteCount() {
        return completeCount;
    }

    public void setCompleteCount(int completeCount) {
        this.completeCount = completeCount;
    }




}





}
Ravi
  • 4,872
  • 8
  • 35
  • 46
  • http://stackoverflow.com/questions/5314813/json-gson-fromjson-java-objects – Ben Dale Aug 30 '13 at 09:39
  • can you please explain how to use the inner classes in such cases – Ravi Aug 30 '13 at 09:45
  • @Chintan no its not solved I was already doing what was given in the above link and i can see the list of inner has 3 elements but variables of inner class always have null value – Ravi Aug 30 '13 at 10:07
  • @Ravi, try my answer. This will solve your problem. I also added comments when to use members and inner classes. :) – Chintan Rathod Aug 30 '13 at 10:09

1 Answers1

2

Try following class for response.

public class Character {

    /**
     * Take array of class when you found "[","]" inside json response
     */
    ArrayList<Data> data;

    /**
     * take normal values as members
     */
    String action;
    int results;
    String message;
    boolean success;

    /**
     * When you will use array, don't forgot to implement empty contructor,
     * which initialize that array
     */
    public Character() {
        data = new ArrayList<Character.Data>();
    }

    /**
     * There will be only "get" methods, no setter method used in GSON bean
     * 
     * @return
     */
    public String getAction() {
        return action;
    }

    public String getMessage() {
        return message;
    }

    public int getResults() {
        return results;
    }

    public boolean getSuccess() {
        return success;
    }

    public ArrayList<Data> getData(){
            return data;
    }

    @Override
    public String toString() {
        String str = "Action : " + action + "\nMessage : " + message
                + "\nResults : " + results + "\nSuccess : " + success;
        str += "\n";
        for (Data d : data) {
            str += "\nTitle : " + d.getTitle();
            str += "\nComplete Count : " + d.getCompleteCount();
            str += "\nId : " + d.getId();
        }
        return str;
    }

    /**
     * as you have => "data":[...] => in your json response, you need to create
     * a class for that
     */
    public class Data {

        /**
         * take normal values as members
         */
        private String title;
        private int id;
        private int completeCount;

        public String getTitle() {
            return title;
        }

        public int getId() {
            return id;
        }

        public int getCompleteCount() {
            return completeCount;
        }
    }

}

Output

To print output, use following code. I have override toString() function.

Gson gson = new Gson();
Character character = gson.fromJson(strResponse, Character.class);
Log.d("Home", character.toString());

08-30 15:36:59.279: DEBUG/Home(10022): Action : param
08-30 15:36:59.279: DEBUG/Home(10022): Message : Action completed successfully.
08-30 15:36:59.279: DEBUG/Home(10022): Results : 3
08-30 15:36:59.279: DEBUG/Home(10022): Success : true
08-30 15:36:59.279: DEBUG/Home(10022): Title : test1
08-30 15:36:59.279: DEBUG/Home(10022): Complete Count : 0
08-30 15:36:59.279: DEBUG/Home(10022): Id : 2
08-30 15:36:59.279: DEBUG/Home(10022): Title : test2
08-30 15:36:59.279: DEBUG/Home(10022): Complete Count : 0
08-30 15:36:59.279: DEBUG/Home(10022): Id : 3
08-30 15:36:59.279: DEBUG/Home(10022): Title : test2
08-30 15:36:59.279: DEBUG/Home(10022): Complete Count : 0
08-30 15:36:59.279: DEBUG/Home(10022): Id : 3

Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
  • @Ravi I tried, and i am getting **test1** as output.. there is wrong with your code. recheck. :) – Chintan Rathod Aug 30 '13 at 10:31
  • you are right thanks man for the help and explaination its working now – Ravi Aug 30 '13 at 10:37
  • If i have like [..] within date : [..] then i have to create a list and for that list a class within the inner class (DATA) or just another inner class to the main class(CHARACTER) ?? – Ravi Aug 30 '13 at 13:10
  • If date[..] is there, then you need to create one another class inside Character (not actually inner class of DATA), and you need to create list in DATA and as you know now, contructor and other stuff. :) – Chintan Rathod Aug 30 '13 at 13:22
  • 1
    Thanks..Now i think i can handle all kinds of json – Ravi Aug 30 '13 at 13:36
  • @ Chintan Rathod can you plz suggest a way to parse data like above which also has paging , like facebook friendlist – Ravi Sep 25 '13 at 10:12
  • @ Chintan Rathod i will provide the data as soon as it is available if you can give me your email id i will mail you . Thanks – Ravi Sep 25 '13 at 10:31