【发布时间】:2026-02-07 03:05:02
【问题描述】:
我看到人们使用toString() 来获取JsonNode 的文本值,当它是ObjectNode 而不是ValueNode 时,尤其是当某些节点的内容也是JSON 字符串时;我们可以构造另一个 JsonNode 来更深入地遍历内部树。
JsonNode shippingInfo = null;
JsonNode brand = null;
ArrayNode included = (ArrayNode)details.get("included");
for (JsonNode node: included) {
if ("offers".equals(node.get("type").asText()) &&
orderOffer.getOfferId().toString().equals(node.get("id").asText())) { // asText() will return empty string "", because it is not ValueNode.
shippingInfo = node.get("attributes").get("shippingSellerMethod");
} else if ("brands".equals(node.get("type").asText())) {
brand = node.get("attributes");
}
}
我知道它有用但丑陋。我想知道是否还有另一种杰克逊式的方式来获得价值,并不总是get(node_name).toString()。
【问题讨论】:
-
“不,没有。”也算作有效答案。
标签: java json jackson jackson-databind