使用 java 比较两个 JSON 文件

Comparing two JSON files using java

我有两个 JSON 文件,名为“pickevent1”和“pickevent2”。我必须比较两个文件是否匹配;如果他们不匹配,我需要知道他们在哪里不匹配。

pickevent1

 {
     "pickEventActivities": [{
         "orderId": "215",
         "lineNbr": 0,
         "pick": "EACH",
         "activations": [{
             "activationType": "Si",
             "activationValue": "31"
         }]
      }]
  }

pickevent2

{
    "pickEventActivities": [{
        "orderId": "115",
        "lineNbr": 1,
        "pick": "Hello",
        "activations": [{
            "activationType": "Bi",
            "activationValue": "3"
        }]
    }]
}

我创建了一个 pick 事件 POJO class:

@JsonRootName(value = "pickEventActivities")
@Data
@JsonPropertyOrder({ "orderId", "lineNbr", "pick"})
class PickEvent {
    String orderId;
    String lineNbr;
    String pick;
    List<Activation> activations;
}

和一个激活 POJO class:

@Data
@JsonPropertyOrder({ "activationType", "activationValue"})
public class Activation {
    String activationType;
    String activationValue;
}

为了确保它有效,我创建了一个测试 class:

public void compareJson() throws Exception {
    final ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.configure(ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
    
    PickEvent result1 = objectMapper.readValue(new File("src/../pickevent1.json"), PickEvent.class);
    PickEvent result2 = objectMapper.readValue(new File("src/../pickevent2.json"), PickEvent.class);
    
    assertEquals(result1, result2);
}

但是当我做 assertSame(result1,result2) 时,它给了我 json 值空值:

Exception in thread "main" java.lang.AssertionError: expected same:<PickEvent(orderId=null, lineNbr=null, pick=null, activations=null)> was not:<PickEvent(orderId=null, lineNbr=null, pick=null, activations=null)>
    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.failNotSame(Assert.java:828)
    at org.junit.Assert.assertSame(Assert.java:771)
    at org.junit.Assert.assertSame(Assert.java:782)
    at JsonDiff.PickEventDiff.comparejson(PickEventDiff.java:26)
    at JsonDiff.PickEventDiff.main(PickEventDiff.java:32)

应该是断言错误,但是测试成功了。

您正在尝试读取一个 PickEvent 对象,但您实际上是在向那里发送一个列表。

请将您的 json 更改为

{
    "pickEventActivities": {
        "orderId": "115",
        "lineNbr": 1,
        "pick": "Hello",
        "activations": [{
            "activationType": "Bi",
            "activationValue": "3"
        }]
    }
}

或尝试将代码更改为

List<PickEvent> list1 = objectMapper.readValue(new File("src/../pickevent1.json"), new ParameterizedTypeReference<PickEvent>(){});

It should give me an assertion error, but the test succeeds.

因为你使用objectMapper.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);。其实解析过程中出现了异常

尝试:

    public void compareJson() throws Exception {
        final ObjectMapper objectMapper = new ObjectMapper();
        Wrapper wrapper = objectMapper.readValue(new File(""), Wrapper.class);
        Wrapper wrapper2 = objectMapper.readValue(new File(""), Wrapper.class);
        System.out.println(wrapper.equals(wrapper2));
    }

    @Data
    static class Wrapper {
        List<PickEvent> pickEventActivities;
    }