网站无法打开网页是怎么回事,wordpress5.0新版如何发布文章,网站备案要营业执照原件吗,自己做付费网站最近看到有个同事在遍历json数组的时候#xff0c;用for循环写了一层有一层#xff0c;那么是否有简便的写法呢#xff1f;当然有了#xff0c;下面就有用流的行驶#xff0c;优雅的遍历数组#xff0c;获取我们想要的数据
public static void main(String[] args) {Str…最近看到有个同事在遍历json数组的时候用for循环写了一层有一层那么是否有简便的写法呢当然有了下面就有用流的行驶优雅的遍历数组获取我们想要的数据
public static void main(String[] args) {String structure [{\key\:\111\,\isRequired\:true,\isNumber\:true,\isFixed\:true},{\key\:\2\,\isFixed\:true},{\key\:\3\,\isNumber\:true},{\key\:\4\,\isRequired\:true}];JSONArray jsonArray;try {jsonArray JSON.parseArray(structure);} catch (JSONException e) {System.err.println(Invalid JSON format: e.getMessage());return;}ListString unitList Optional.ofNullable(jsonArray).orElse(new JSONArray()).stream().filter(item - item instanceof JSONObject).map(item - {JSONObject jsonObject (JSONObject) item;String key jsonObject.getString(key);return key;}).collect(Collectors.toList());System.out.println(unitList);
}