[英]JsonArray loop using Streams
I'm trying to loop a JsonArray using Streams in java, I have looped a JsonObject Using the following code 我正在尝试使用Java中的Streams循环JsonArray,我已经使用以下代码循环了JsonObject
jsonObject.entrySet().stream()
but there isn't no entrySet()
class for JsonArray, How ever I tried using the iterator method but it only printed out one of the JsonObject inside of the JsonArray here is my attempt 但是没有JsonArray的
entrySet()
类,但是我尝试过使用iterator方法,但是它仅在JsonArray内打印出JsonObject之一。
.map(JsonArray::iterator)
.map(Iterator::next)
.forEach(System.out::println);
Edit: I'm getting the JsonArray from a JsonObject stream 编辑:我从一个JsonObject流中获取JsonArray
.map(JsonArray.class::cast)
//need to handle jsonArray
These are the utility functions which converts iterator into stream. 这些是将迭代器转换为流的实用程序函数。 use these functions to covert your iterator into streams.
使用这些功能可以将迭代器转换为流。
public static <T> Stream<T> asStream(Iterator<T> sourceIterator) {
return asStream(sourceIterator, false);
}
public static <T> Stream<T> asStream(Iterator<T> sourceIterator, boolean parallel) {
Iterable<T> iterable = () -> sourceIterator;
return StreamSupport.stream(iterable.spliterator(), parallel);
}
like : 喜欢 :
asStream(jsonObject.keys())
.map(JsonObject::getJSONObject)
.foreach(//do what you want);
OR 要么
asStream(jsonObject.keys()).forEach();
Use StreamSupport.stream(jsonArray.spliterator(), false)
to get a Stream
of json array. 使用
StreamSupport.stream(jsonArray.spliterator(), false)
获取json数组Stream
。
So your code will look like:- 因此您的代码将如下所示:
StreamSupport.stream(jsonArray.spliterator(), false)
.forEach(....);
jsonObject.entrySet().stream()
.map(entery -> {
return StreamSupport.stream(((JsonArray) entery.getValue()).spliterator(), false);
})
.forEach(stream->stream.forEach(System.out::println));
Using stream support you can map/convert your jsonArray to stream and then in forEach you get the stream for each jsonArray then you can stream them in that forEach 使用流支持,您可以将jsonArray映射/转换为流,然后在forEach中获取每个jsonArray的流,然后可以在该forEach中对其进行流传输
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.