我需要通过忽略一些字段来比较两个json字符串
我目前正在使用org.SkyScreamer的JSONAssert进行比较,但不会忽略任何属性。
Json 1:
{
"contributions": [
[
{
"order" : 1,
"contributorId" : "1980"
}
]
]
}Json 2:
{
"contributions": [
[
{
"order": 1,
"contributorId" : "5789"
}
]
]
}ArrayValueMatcher<Object> arrValMatch1 = new ArrayValueMatcher<>(new CustomComparator(
JSONCompareMode.NON_EXTENSIBLE,
new Customization("contributions[0][*].contributorId",(o1, o2) -> true)));
Customization arrayValueMatchCustomization = new Customization("contributions", arrValMatch1);
CustomComparator customArrayValueComparator = new CustomComparator(
JSONCompareMode.NON_EXTENSIBLE,
arrayValueMatchCustomization);
assertEquals(subJson1, json2, customArrayValueComparator);我希望上面的方案能够通过。但是它失败了,因为
Exception in thread "main" java.lang.AssertionError: contributions[0][contributorId=1980]
Expected: a JSON object
but none found
; contributions[0][contributorId=5789]
Unexpected: a JSON object发布于 2021-01-08 03:20:39
使用*.contributorId而不是contributions[0][*].contributorId
https://stackoverflow.com/questions/56277056
复制相似问题