0

I am trying to iterate through JsonNode tree, and I have written a following snippet that iterate over children nodes of root node and gets the text out of it, which I assume will be a field name.

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);
}

I have read 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.

The other most upvoted answer works, but I was wondering if the above snippet's assumption is still valid.

Community
  • 1
  • 1
THIS USER NEEDS HELP
  • 3,136
  • 4
  • 30
  • 55
  • The accepted answer states _Since `JsonNode#iterator()` does not include keys_ with a link to the javadoc for more details. – Sotirios Delimanolis Aug 11 '16 at 01:11
  • If you're confused about the foreach statement, [here's](http://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work) how it works in relation to `iterator()`. – Sotirios Delimanolis Aug 11 '16 at 01:11

1 Answers1

1

No.

The javadoc of JsonNode#iterator() states

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

And the javadoc of JsonNode#elements() states

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.

So the elements returned are the values of each key-value pair of an object node. For array nodes, it's the array elements.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Hm that's a shame. I really liked how clean that code snippet looked. Also, I feel like I am missing a vital information about Java. Why was the original answer accepted if the `iterator` cannot be iterated through `for` loop? – THIS USER NEEDS HELP Aug 11 '16 at 01:06
  • @THISUSERNEEDSHELP I don't understand that last question. If a type implements `Iterable`, a value of that type can be used in a foreach statement. – Sotirios Delimanolis Aug 11 '16 at 01:08
  • @THISUSERNEEDSHELP Also, I realize now that the answer there already contains my answer. – Sotirios Delimanolis Aug 11 '16 at 01:10
  • In [this post](http://stackoverflow.com/questions/7653813/jackson-json-get-node-name-from-json-tree), the accepted answer loops through `Iterator`, but I thought `Iterator` itself cannot be iterated through `foreach` loop as indicated [here](http://stackoverflow.com/questions/2598219/java-why-cant-iterate-over-an-iterator). The class need to implement `Iterable` as you said, which is not guaranteed for either `fields()` or `fieldNames()` since they are simply `Iterators`, but do not necessarily implement `Iterable`. – THIS USER NEEDS HELP Aug 11 '16 at 01:15
  • 1
    @THISUSERNEEDSHELP So `JsonNode` implements `Iterable`. But, yes, you are correct about the others. You've posted a comment already. Matt is pretty active. They should be able to fix that. Otherwise, I will for them. – Sotirios Delimanolis Aug 11 '16 at 01:16
  • Thanks! Yeah sorry about the confusion. I meant to write `foreach` instead of `for` loop. – THIS USER NEEDS HELP Aug 11 '16 at 01:24