我正在尝试遍历Jsonnode,但是根jsonNode正在复制数据。试图找出但不确定我遗漏了什么问题。我将尝试在下面解释这个问题。
我得去看杰克逊的API。
Json块是:
{“查询”:[
{
"id": "keyword",
"values": [
"test"
]
},{
"id": "include",
"values": [
false
]
}
]
}我的Java代码块是迭代器fieldNames = root.fieldNames();while (fieldNames.hasNext()) {
String fieldName = fieldNames.next();
if (fieldName.equalsIgnoreCase("queries")) {
nameNode =root.get(fieldName);
}
JsonNode nameNode = root.get("queries");
for (JsonNode node : nameNode) {
JsonNode elementId = node.path("id").asText();
if (!elementId.isEmpty() && elementId.equalsIgnoreCase("include")) {
check = true;
include = node;
}
}
When debug comes to line for (JsonNode node : nameNode) { , node value is "id": "keyword", "values": [ "test" ] and nameNode is the json shown above but when it comes to next line which is " node.path("id").asText();"nameNode变量追加"id":“关键字”,“值”:“测试”2次。
现在,json是原始的json,添加了2次"id":"keyword","values":“concurrentModificationException”,并给出了测试。
发布于 2017-07-27 06:04:07
将变量节点更改为objNode,因为节点在jackson中可能是预定义的值,您也可以尝试将每个变量设置为final
https://stackoverflow.com/questions/45334769
复制相似问题