1

我需要通过忽略一些字段来比较两个 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
4

1 回答 1

1

使用*.contributorId代替contributions[0][*].contributorId

于 2021-01-07T19:20:38.900 回答