忽略jsonassert中的特定属性/字段

时间:2019-05-23 14:05:48

标签: java jsonassert

我需要通过忽略某些字段来比较两个json字符串

我目前正在使用来自org.SkyScreamer的JSONAssert进行比较,但不会忽略任何属性。

杰森1:

{
  "contributions": [
    [
      {
        "order" : 1,
        "contributorId" : "1980"
      }
    ]
  ]
}

杰森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

1 个答案:

答案 0 :(得分:0)

使用 *.contributorId 而不是 contributions[0][*].contributorId