【发布时间】:2012-05-29 16:55:54
【问题描述】:
我有这样的服务器返回的 json:
[{"id":1,"group":[{"id":1,"subGroup":[{"id":1,"item":"X"}]}]}]
如何获取数组组和子组?
我在 Android 客户端上使用 restlet。
谢谢大家。
【问题讨论】:
标签: java android arrays deserialization restlet
我有这样的服务器返回的 json:
[{"id":1,"group":[{"id":1,"subGroup":[{"id":1,"item":"X"}]}]}]
如何获取数组组和子组?
我在 Android 客户端上使用 restlet。
谢谢大家。
【问题讨论】:
标签: java android arrays deserialization restlet
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".
您也可以重复此模式以获得“子组”。
【讨论】:
我解决了这个问题如下:
先改变服务器的json:
来自:
[{"id":1,"group":[{"id":1,"subGroup":[{"id":1,"item":"X"}]}]}]
到
{"array":[{"id":1,"group":[{"id":1,"subGroup":[{"id":1,"item":"X"}]}]}]}
我在 Android 客户端中的第二个操作是:
获取第一个数组的类,带有firstArray "array"的类ServerModel:
public class ServerModel implements Serializable {
private static final long serialVersionUID = 1L;
private FirstArray[] array;
public ServerModel() {
}
public ServerModel(FirstArray[] array) {
this.array = array;
}
}
第二个数组“组”的第三类:
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;
}
}
第四个类与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;
}
}
最后是item“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;
}
}
谢谢大家的帮助!!!
【讨论】: