将多维 JSON 数组解析为 Java 中的平面 int 列表
Parse multi-dimensional JSON array to flat int list in Java
我正在构建一个小型微服务来使用 Java & Vert.x
实现几个排序算法
我的一个要求是处理像 [5, [4, 3, 2], 1, [[0]]]
这样的嵌套列表
请求正文是一个 JSON 对象,例如:
{"arr": [5, [4, 3, 2], 1, [[0]]]}
如何将带有嵌套列表的 JSON 对象/JSON 数组解析为 Java 中的平面列表?
// This is how I handle simple lists
private void doBubbleSort(RoutingContext routingContext) {
JsonObject json = routingContext.getBodyAsJson();
JsonArray jsonArray = json.getJsonArray("arr");
// How do I get the size of the list if it is multi-dimensional
int size = jsonArray.size();
int[] unsortedList = new int[size];
for (int i = 0; i < size; i++) {
// Here I want to check whether the current item is an int or
// another nested list. if it is a list, i want to loop over it
// and also add it to the result
unsortedList[i] = jsonArray.getInteger(i);
}
...
}
我要的结果:
int[5, 4, 3, 2, 1, 0]
我知道我需要检查当前值是 int 类型还是 list 类型,但努力让它处理从 JSON 到 int 到 list 的类型转换。
在 Python 中,我可以在不进行类型转换的情况下执行此操作。
def flatten_list(arr: list):
nested_arr = deepcopy(arr)
while nested_arr:
sublist = nested_arr.pop(0)
if isinstance(sublist, int):
yield sublist
if isinstance(sublist, list):
nested_arr = sublist + nested_arr
根据您的回答尝试以下操作:
private void doBubbleSort(RoutingContext routingContext) {
JsonObject json = routingContext.getBodyAsJson();
JsonArray jsonArray = json.getJsonArray("arr");
List<?> list = jsonArray.getList();
List<Integer> flatList = list.stream()
.map(this::getOrFlatten)
.flatMap(List::stream)
.collect(Collectors.toList());
// convert List<Integer> to int[]
// ...
}
private List<Integer> getOrFlatten(Object o) {
if(o instanceof Integer) {
return Collections.singletonList((Integer) o);
} else if(o instanceof List) {
List<?> list = (List) o;
return list.stream()
.map(this::getOrFlatten)
.flatMap(List::stream)
.collect(Collectors.toList());
} else {
throw new IllegalArgumentException(o.getClass() + " is not supported at getOrFlatten");
}
}
Here you can find how to convert List to int []
我正在构建一个小型微服务来使用 Java & Vert.x
实现几个排序算法我的一个要求是处理像 [5, [4, 3, 2], 1, [[0]]]
请求正文是一个 JSON 对象,例如:
{"arr": [5, [4, 3, 2], 1, [[0]]]}
如何将带有嵌套列表的 JSON 对象/JSON 数组解析为 Java 中的平面列表?
// This is how I handle simple lists
private void doBubbleSort(RoutingContext routingContext) {
JsonObject json = routingContext.getBodyAsJson();
JsonArray jsonArray = json.getJsonArray("arr");
// How do I get the size of the list if it is multi-dimensional
int size = jsonArray.size();
int[] unsortedList = new int[size];
for (int i = 0; i < size; i++) {
// Here I want to check whether the current item is an int or
// another nested list. if it is a list, i want to loop over it
// and also add it to the result
unsortedList[i] = jsonArray.getInteger(i);
}
...
}
我要的结果:
int[5, 4, 3, 2, 1, 0]
我知道我需要检查当前值是 int 类型还是 list 类型,但努力让它处理从 JSON 到 int 到 list 的类型转换。
在 Python 中,我可以在不进行类型转换的情况下执行此操作。
def flatten_list(arr: list):
nested_arr = deepcopy(arr)
while nested_arr:
sublist = nested_arr.pop(0)
if isinstance(sublist, int):
yield sublist
if isinstance(sublist, list):
nested_arr = sublist + nested_arr
根据您的回答尝试以下操作:
private void doBubbleSort(RoutingContext routingContext) {
JsonObject json = routingContext.getBodyAsJson();
JsonArray jsonArray = json.getJsonArray("arr");
List<?> list = jsonArray.getList();
List<Integer> flatList = list.stream()
.map(this::getOrFlatten)
.flatMap(List::stream)
.collect(Collectors.toList());
// convert List<Integer> to int[]
// ...
}
private List<Integer> getOrFlatten(Object o) {
if(o instanceof Integer) {
return Collections.singletonList((Integer) o);
} else if(o instanceof List) {
List<?> list = (List) o;
return list.stream()
.map(this::getOrFlatten)
.flatMap(List::stream)
.collect(Collectors.toList());
} else {
throw new IllegalArgumentException(o.getClass() + " is not supported at getOrFlatten");
}
}
Here you can find how to convert List to int []