在Java中遍历JSON数组,你可以使用以下方法:
1. 使用`forEach`循环:
JSONArray jsonArray = new JSONArray("[1, 2, 3, 4, 5]"); jsonArray.forEach(element -> System.out.println(element));
2. 使用`for`循环和`get`方法:
JSONArray jsonArray = new JSONArray("[1, 2, 3, 4, 5]"); for (int i = 0; i < jsonArray.length(); i++) { System.out.println(jsonArray.get(i)); }
3. 使用流API(Java 8及以上版本):

JSONArray jsonArray = new JSONArray("[1, 2, 3, 4, 5]"); jsonArray.stream().forEach(System.out::println);
4. 使用`iterator`迭代器:
JSONArray jsonArray = new JSONArray("[1, 2, 3, 4, 5]"); Iterator
-- 展开阅读全文 --