Java 获取JsonNode文本值的正确方法
我看到人们使用Java 获取JsonNode文本值的正确方法,java,json,jackson,jackson-databind,Java,Json,Jackson,Jackson Databind,我看到人们使用toString()获取JsonNode的文本值,当它是ObjectNode而不是ValueNode时,尤其是当某些节点的内容也是JSON字符串时;我们可以构造另一个JsonNode来深入内部树 JsonNode shippingInfo = null; JsonNode brand = null; ArrayNode included = (ArrayNode)details.get("included"); for (JsonNode node: included) {
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");
}
}
我知道这很有用,但很难看。我想知道是否还有其他更类似Jackson的方法来获取值,而不是总是get(node\u name).toString()
“不,没有。”也是一个有效的回答。