arrays - 如何在 Java 中将 Json Arraysize 修剪为 1
问题描述
我有一个包含许多节点的 Json,其中一些节点包含大小大于 1 的数组。我正在尝试编写一些代码来将数组大小修剪为 1,并确保所有数组元素的所有属性都包含在该数组中元素。然后,我将使用“清理”的 json 生成一个 avro 模式,以与 kafka 流 API 一起使用。实际的 json 非常大,所以为了使其易于管理,我正在尝试这样做。
下面的代码是使用 gson 尝试的,但我对 gson 或 jackson 没有偏好。
这是我的尝试
private static void traverseJsonObject(JsonObject source, JsonObject compactedJson, String[] currentJsonPath) {
String parentKey = currentJsonPath[0];
source.keySet().forEach(key ->
{
Object keyValue = source.get(key);
if (keyValue instanceof JsonObject) {
currentJsonPath[0] += "."+key;
traverseJsonObject((JsonObject) keyValue, compactedJson, currentJsonPath);
}
if (keyValue instanceof JsonArray) {
JsonArray srcArray = (JsonArray) keyValue;
// get the size of the array
// if the array size is 0 or 1
// continue
// if array size is > 1
// call compactArray method, passing the value ( jsonarray )
// this method should return an array of size 1
// remove the node from compactedJson
// add the returned array to compactedJson
if(srcArray.size() > 1) {
JsonObject firstArrayElement = (JsonObject) srcArray.get(0);
JsonArray merged = compactArray(srcArray, firstArrayElement);
//*********** **This is where my error occurs**
// I would ideally like to build a jsonpath for the key at this point,
// but I am NOT able to build a correct jsonpath here
JsonElement elementAtKey = compactedJson.get(key);
JsonArray compactedArrayAtKey = (JsonArray) compactedJson.get(key);
int srcArraySize = srcArray.size();
for (int i = 0; i < srcArraySize; i++) {
compactedArrayAtKey.remove(i);
}
compactedArrayAtKey.addAll(merged);
compactedJson.add(key, compactedArrayAtKey);
} //remove the key node
// add jsonarray at key
}
// iterate over keys of input json
// If the value for the key is of type jsonarray
// get the size of the array
// if the array size is 0 or 1
// continue
// if array size is > 1
// call compactArray method, passing the value ( jsonarray )
// this method should return an array of size 1
// remove the node from compactedJson
// add the returned array to compactedJson
});
}
private static JsonArray compactArray(JsonArray multiDimArray, JsonObject firstArrayElement) {
IntStream.range(0, multiDimArray.size()).forEach(index -> {
JsonElement srcElem = multiDimArray.get(index);
JsonObject srcAtIndex = (JsonObject) srcElem;
Set<String> sourceKeySet = srcAtIndex.keySet();
for (String key : srcAtIndex.keySet()) {
if(!firstArrayElement.has(key)) {
firstArrayElement.addProperty(key, String.valueOf(srcAtIndex.get(key)));
}
}
});
JsonArray oneElementArray = new JsonArray();
oneElementArray.add(firstArrayElement);
return oneElementArray;
}
这是我的错误发生的地方
JsonElement elementAtKey = compactedJson.get(key);
示例 JSON
{
"TopmostNode" {
"Level1-1"{
"level2-1" {
"attr2-1-1": "value2-2-1",
},
"level2-2" {
"attr2-2-1": "value2-2-1",
"level3-1": [
{
"attr3-1-1-1": "value3-1-1-1"
"attr3-1-1-2": "value3-1-1-2"
},
{
"attr3-1-1-1": "value3-1-1-1"
"attr3-1-2-1": "value3-1-2-1"
}
]
}
}
}
}
预期产出
{
"TopmostNode" {
"Level1-1"{
"level2-1" {
"attr2-1-1": "value2-2-1",
},
"level2-2" {
"attr2-2-1": "value2-2-1",
"level3-1": [
{
"attr3-1-1-1": "value3-1-1-1"
"attr3-1-1-2": "value3-1-1-2"
"attr3-1-2-1": "value3-1-2-1"
}
]
}
}
}
}
理想情况下,我现在想为密钥构建一个 jsonpath,但我无法在这里构建一个正确的 jsonpath
如何在遍历 json 对象时构建 jsonpath 或
是否有另一种方法可以将所有数组修剪为大小 1?
谢谢
解决方案
推荐阅读
- android - 带有折叠工具栏的Android重叠回收器视图
- c# - 如何在MS WORD c#中填写表格
- java - java转换List的方法
列出 其中 T 可以是整数或长整数 - javascript - 在javascript中将对象转换为数组对象
- google-chrome-extension - Chrome.i18n 不适用于 Gmail 更改语言
- swift - 当输入为 nil 时,Swift input == nil 会产生 false
- javascript - 当父元素设置为阻塞时,如何将子元素显示属性设置为无
- python - 具有返回变量函数 Maya Python 的 ScriptJob
- node.js - 遵循指南后,材料表不会排序
- c# - 获取 Foundation.NSErrorException:错误域 = NSURLErrorDomain 代码 = -1009 “互联网连接似乎处于脱机状态。” 在 Xamarin 窗体 iOS 上