Jackson json: json tree intersection node by node

I have many text files containing json data and I use the new ObjectMapper () method. readTree () in the Jackson json parser to parse json data for DOM trees.

Say now, now I have two DOM trees - t1 and t2. Each tree will have many child nodes, which in turn will have many child nodes.

What I would like to do is go through the tree t1 node on node and compare each node in t1 with each node in t2. I know that the Jackson json parser allows me to request specific nodes, but how do I go through the node tree on node?

+3
source share
3 answers

JsonNode.iterator() node ( , ). , node JsonNode.isArray JsonNode.isObject , . , , .

+2

t1 t2, , t1.equals(t2). , t1 t2 - JsonNode, equals.

+2
 boolean NodesEqual(JsonNode n1, JsonNode n2) {
  if(n1.size()!=n2.size())return false;
  // ... other equality checks, like name, data type, etc
  for(int i=0;i<n.size();i++){
    JsonNode child1 = n1.get(i);
    JsonNode child2 = n2.get(i);
    if(!NodesEqual(child1,child2)) return false;
  } 
  return true;
 }

These are recursive, so massive or deeply nested documents can have problems, but this should work fine in normal cases.

+1
source

All Articles