我有一个 JSON 数组,我想从中选择数据。我想获得所有科目,但我不知道该怎么做。
代码:
JSONObject jsonObject = new JSONObject(thatarray);
JSONArray jsonArray = jsonObject.getJSONArray("response");
int arrSize = jsonArray.length();
List<Integer> sub = new ArrayList<Integer>(arrSize);
for (int i = 0; i < arrSize; ++i) {
jsonObject = jsonArray.getJSONObject(i);
System.out.println("Output: " + jsonObject.toString());
}
最佳答案
实际上“response”是jsonObject,“data”是jsonArray.. 你可以通过查看 {} 和 [] 来区分 jsonArray 和 jsonObject... 希望它会有所帮助:)
我自己在 JSON 上尝试了下面的代码,它工作正常。
try {
JSONObject jsonObject = new JSONObject(thatarray);
jsonObject = jsonObject.getJSONObject("response");
JSONArray jsonArray = jsonObject.getJSONArray("data");
JSONArray jsonArraysubject;
for (int i = 0; i < jsonArray.length() - 1; i++) {
jsonObject = jsonArray.getJSONObject(i);
jsonArraysubject = jsonObject.getJSONArray("subjects");
Log.d("MyLog", jsonArraysubject + "");
}
} catch (JSONException e) {
e.printStackTrace();
}
关于java - 从 JSON 数组获取 JSON 数组 Java android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37629814/