我试图弄清楚如何通过指定该数组的路径来更改 JSON 数组中的现有对象。
问题是我不知道如何指定数组的路径以便可以更改它。我尝试过的每种方法都没有示例说明如何更改 ObjectNode 中的数组。
这是我现在的代码,在其中我尝试通过指针表示法指定数组的路径来获取和更改数组,但是,这总是不返回数组:
构建 POJO 类不是一种选择。
我的 json 结构中的数组如下:
{
RESULTS: {
ATTACHMENTS: {
ATTACHMENT: [{object}]
}
}
}
代码:
private JsonNode attachmentConversion(JsonNode object){
final String ATTACHMENT_POINTER = "/RESULTS/ATTACHMENTS/ATTACHMENT"; //Path to the array
ObjectNode OUTPUT = object.deepCopy();
OUTPUT.remove("DETAILS");
//Validate is an array - It properly fetches the array
if(object.at(ATTACHMENT_POINTER).isArray()){
int counter=0;
//Loop through attachment array - It properly fethes each object in the array
for(final JsonNode objNode : object.at(ATTACHMENT_POINTER)){
ObjectNode objectNode = objNode.deepCopy();
OUTPUT.withArray(ATTACHMENT_POINTER) //HERE IS THE PROBLEM - How do I specify the path to the array I want to replace with a new object in the withArray api? This always returns back as an empty array, even though the loop above properly fetches the array elements.
.set(counter
, objectNode
.replace("ATTACH_CONTENT"
, xmlToJson.parseXmlToJson(objNode.get("ATTACH_CONTENT")
.asText())));
counter = counter+1;
}
}
return new ObjectMapper()
.createObjectNode()
.set("customertopology", OUTPUT);
}