如何获取jsonarray中的数组

2025-01-03 15:03:45
推荐回答(1个)
回答1:

需要写一个方法,把json数据转换成list集合数据
public static List jsonToBean(String data, Object bean) {
List list = new ArrayList();

try {
JSONArray array;

try {
array = new JSONArray(data);
for (int i = 0; i < array.length(); i++) {
Object toBean = getBean(bean);
JSONObject ob = new JSONObject();
ob = (JSONObject) array.get(i);
toBean = jsonStrToBean(ob, toBean);
list.add(toBean);

}
return list;
} catch (JSONException e) {
try {
Object obj = null;
JSONObject jsonObj = new JSONObject(data);
Object toBean = getBean(bean);
toBean = jsonStrToBean(jsonObj, toBean);
list.add(toBean);
return list;
} catch (JSONException e1) {
log.error("Error covert String to JSONObject", e);
e1.printStackTrace();
}
e.printStackTrace();
log.error("Error covert String to JSONArray", e);
}

} catch (SecurityException e) {
e.printStackTrace();
}
return list;
}
然后取出list集合数据中的值应该没问题了吧。