[英]Comparing two JSON files using java
I have two JSON files, called "pickevent1" and "pickevent2".我有两个 JSON 文件,称为“pickevent1”和“pickevent2”。 I have to compare if both files are matching;我必须比较两个文件是否匹配; if they don't match, I need to know where they don't match.如果他们不匹配,我需要知道他们不匹配的地方。
pickevent1挑选事件1
{
"pickEventActivities": [{
"orderId": "215",
"lineNbr": 0,
"pick": "EACH",
"activations": [{
"activationType": "Si",
"activationValue": "31"
}]
}]
}
pickevent2挑选事件2
{
"pickEventActivities": [{
"orderId": "115",
"lineNbr": 1,
"pick": "Hello",
"activations": [{
"activationType": "Bi",
"activationValue": "3"
}]
}]
}
I created a pick event POJO class:我创建了一个选择事件 POJO class:
@JsonRootName(value = "pickEventActivities")
@Data
@JsonPropertyOrder({ "orderId", "lineNbr", "pick"})
class PickEvent {
String orderId;
String lineNbr;
String pick;
List<Activation> activations;
}
and a Activation POJO class:和激活 POJO class:
@Data
@JsonPropertyOrder({ "activationType", "activationValue"})
public class Activation {
String activationType;
String activationValue;
}
To make sure it works, I created a test class:为了确保它有效,我创建了一个测试 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);
}
But when I am doing assertSame(result1,result2)
its giving me null for json values:但是当我在做assertSame(result1,result2)
时,它给了我 null 的 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)
It should give me an assertion error, but the test succeeds.它应该给我一个断言错误,但测试成功。
It should give me an assertion error, but the test succeeds.它应该给我一个断言错误,但测试成功。
Because you use objectMapper.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
因为您使用objectMapper.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
. . In fact, an exception occurred during the parsing process.事实上,在解析过程中发生了异常。
Try:尝试:
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;
}
You are trying to read a PickEvent Object but you're actually sending a list there.您正在尝试阅读 PickEvent Object 但您实际上是在发送列表。
Please change your json to请将您的 json 更改为
{
"pickEventActivities": {
"orderId": "115",
"lineNbr": 1,
"pick": "Hello",
"activations": [{
"activationType": "Bi",
"activationValue": "3"
}]
}
}
or try changing your code to或尝试将您的代码更改为
List<PickEvent> list1 = objectMapper.readValue(new File("src/../pickevent1.json"), new ParameterizedTypeReference<PickEvent>(){});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.