0

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.

ademar111190
  • 14,215
  • 14
  • 85
  • 114

2 Answers2

0
String mResponse = "[{"id":1,"group":[{"id":1,"subGroup":[{"id":1,"item":"X"}]}]}]";
JSONArray responseArrayJson = new JSONArray(mResponse); // This creates a JSON array from your response string.
JSONObject objectJson = responseArrayJson.getJSONObject(0); // gets the one and only JSON object in your array.
JSONArray groupArrayJson = objectJson.getJSONArray("group"); // gets the array indexed by "group".

You can repeat this pattern to get "subGroup" as well.

jmhend
  • 537
  • 1
  • 6
  • 16
0

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!!!

ademar111190
  • 14,215
  • 14
  • 85
  • 114