Best way to compare 2 JSON files in Java

后端 未结 10 649
囚心锁ツ
囚心锁ツ 2020-12-13 13:56

How would you suggest this task is approached?

The challenge as i see it is in presenting diff information intelligently. Before i go reinventing the wheel, is there

10条回答
  •  时光取名叫无心
    2020-12-13 14:21

    This only addresses equality, not differences.


    With Jackson.

    ObjectMapper mapper = new ObjectMapper();
    
    JsonNode tree1 = mapper.readTree(jsonInput1);
    JsonNode tree2 = mapper.readTree(jsonInput2);
    
    boolean areTheyEqual = tree1.equals(tree2);
    

    From the JavaDoc for JsonNode.equals:

    Equality for node objects is defined as full (deep) value equality. This means that it is possible to compare complete JSON trees for equality by comparing equality of root nodes.

提交回复
热议问题