繁体   English   中英

(Java)使用Java8将JSONObject的JSONArray展平为[[]]

[英](Java) Flatten JSONArray of JSONObject to [[]] with Java8

我正在使用net.sf.json.JSONArray和net.sf.json.JSONObject。 JSONArray包含多个JSONObject。

基本上是这样的:

[
    {
        "obj1": [
            {
                "ID": 12
                "NAME":"Whatever",
                "XY":[1,2]
            },
            {
                "ID": 34
                "NAME":"Again",
                "XY":[23,43]
            },
            etc
        ]
    },
    { "obj2": repeat}
]

我想用Java 8将其扁平化,即结果:

[
    {
        "obj1": [
                    [12,'Whatever',1,2],
                    [34,'Again',23,43],
                    etc...
        ]
    },
    { "obj2": repeat}
]

尽管您可以通过强制性方式轻松地完成此操作(不带递归),但以函数样式实现可能会更容易。 我不确定我是否擅长Java 8中的惯用功能代码,但您需要:

  • 将项目收集到单个阵列的收集器
static Collector<Object, JSONArray, JSONArray> toJSONArray() {
    return Collector.of(
            JSONArray::new,     // Create a new array
            JSONArray::add,     // Add each element to the target array
            (a1, a2) -> {       // Merge the second array into the first one
                a1.addAll(a2);
                return a1;
            },
            identity()          // Return the populated array itself
    );
}
static Stream<?> flatten(final Object value) {
    return value instanceof Collection
            ? ((Collection<?>) value).stream().flatMap(Q43481457::flatten) // Flatten recursively
            : Stream.of(value);                                            // Otherwise wrap a single element into a stream
}
  • 和主要代码:
@SupressWarnings("unchecked")
final Collection<JSON> jsonArray = (Collection<JSON>) originalJsonArray;
final JSONArray flatJSONArray = jsonArray.stream()
        .map(json -> (Map<?, ?>) json)          // All outer array elements are JSON objects and maps in particular
        .map(jsonObject -> jsonObject.values()  // Recursively flatten the JSON object values
                .stream()
                .flatMap(Q43481457::flatten)
                .collect(toJSONArray())         // ... collecting them to JSON arrays
        )
        .collect(toJSONArray());                // ... which are collected to JSON arrays
System.out.println(flatJSONArray);

输出:

[12, “不管”,1,2],[34, “再”,23,43]]

用法

JSONArray array = ...;
JSONArray flattedArray = flatten(array);

JSONObject map = ...;
JSONObject flattedMap = flatten(map);

履行

当json结构更改时,实现发生了巨大的变化,您可以在github上比较我的提交历史记录。 测试可以告诉您我如何实现您的功能。

public JSONArray flatten(Collection<?> array) {
    return array.stream().flatMap(this::flatting).collect(toJSONArray());
}

private Stream<?> flatting(Object it) {
    if (it instanceof Collection) {
        return ((Collection<?>) it).stream();
    }
    if (it instanceof Map) {
        return Stream.of(flatten((Map<?, ?>) it));
    }
    return Stream.of(it);
}

public JSONObject flatten(Map<?, ?> map) {
    return map.entrySet().stream().collect(
            JSONObject::new,
            (it, field) -> it.put(field.getKey(), flatten(field.getValue())),
            JSONObject::putAll
    );
}

private Object flatten(Object it) {
    if (it instanceof Collection) {
        return ((Collection<?>) it).stream().map(this::flatten)
                                            .collect(toJSONArray());
    }
    if (it instanceof Map) {
        return flatten(((Map<?, ?>) it).values());
    }
    return it;
}

private <T> Collector<T, ?, JSONArray> toJSONArray() {
    return toCollection(JSONArray::new);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM