Search code examples
javaandroidarraysdeserializationrestlet

android restlet get json array


I have the json return from server like this:

[{"id":1,"group":[{"id":1,"subGroup":[{"id":1,"item":"X"}]}]}]

how to I get the arrays group and subgroup?

I'm using restlet on Android Client.

thanks all.


Solution

  • I solved this problem as follows:

    first change the json of server:

    from:

    [{"id":1,"group":[{"id":1,"subGroup":[{"id":1,"item":"X"}]}]}]
    

    to

    {"array":[{"id":1,"group":[{"id":1,"subGroup":[{"id":1,"item":"X"}]}]}]}
    

    Second in Android client I make this:

    a class to get first array, the class ServerModel with firstArray "array":

    public class ServerModel implements Serializable {
        private static final long serialVersionUID = 1L;
        private FirstArray[] array;
        public ServerModel() {
        }
        public ServerModel(FirstArray[] array) {        
            this.array = array;
        }
    }
    

    third the class with secondArray "group":

    public class FirstArray implements Serializable {
        private static final long serialVersionUID = 1L;
        private SecondArray[] group;
        private int id;
        public FirstArray() {
        }
        public FirstArray(int id, SecondArray[] group) {
            this.id = id;
            this.group = group;
        }
    }
    

    fourth the class with thirdArray "subGroup":

    public class SecondArray implements Serializable {
        private static final long serialVersionUID = 1L;
        private Itens[] subGroup;
        private int id;
        public SecondArray() {
        }
        public SecondArray(int id, Itens[] subGroup) {
            this.id = id;
            this.subGroup = subGroup;
        }
    }
    

    and in the last the class of itens "item"

    public class Itens implements Serializable {
        private static final long serialVersionUID = 1L;
        private String item;
        private int id;
        public Itens() {
        }
        public Itens(int id, String item) {
            this.id = id;
            this.item = item;
        }
    }
    

    thanks all for help!!!