假设 Jackson 中根节点的所有子节点都是字段名是否合理?

Is it reasonable to assume that all children of root node in Jackson are field names?

我正在尝试遍历 JsonNode 树,我编写了以下代码段,该代码段遍历根节点的子节点并从中获取文本,我假设这将是一个字段名称。

JsonNode rootNode = new ObjectMapper().readTree(jsonParser);
for (JsonNode node : rootNode){
    String fieldName = node.asText(); // <- is it safe to assume this?
    JsonNode value =  node.get(fieldName);
}

我已阅读this similar post, but the accepted answer simply doesn't work because both fields and fieldNames return iterator, which cannot be iterated through a foreach loop just by itself as far as I know

另一个最受赞誉的答案有效,但我想知道上面片段的假设是否仍然有效。

没有

JsonNode#iterator() 的 javadoc 指出

Same as calling elements(); implemented so that convenience "for-each" loop can be used for looping over elements of JSON Array constructs.

JsonNode#elements() 的 javadoc 指出

Method for accessing all value nodes of this Node, iff this node is a JSON Array or Object node. In case of Object node, field names (keys) are not included, only values. For other types of nodes, returns empty iterator.

所以返回的元素是对象节点的每个键值对的值。对于数组节点,它是数组元素。