使用skyscreamer

时间:2018-01-24 18:48:57

标签: java json jackson

我想在使用skyscreamer比较两个JSON文件时排除某些JSON属性。

测试文件1:

 {
   "Name":"test1",
   "addressList":[
      {
         "street":"123 fake street",
         "postalcode":"33344"
      },
      {
         "street":"99 street avn",
         "postalcode":"23434"
      },
      {
         "street":"900 street city",
         "postalcode":"45100"
      }
   ]
}

测试文件2:

 {
   "Name":"test1",
   "addressList":[
      {
         "street":"123 fake street",
         "postalcode":"33344"
      },
      {
         "street":"88 abc street",
         "postalcode":"23434"
      },
      {
         "street":"900 street city",
         "postalcode":"45100"
      }
   ]
}

如何通过排除addressList.street属性来比较上面的JSON文件?

1 个答案:

答案 0 :(得分:0)

我们可以使用CustomComparator并根据需要编写自己的自定义比较器

 org.skyscreamer.jsonassert.Customization
 org.skyscreamer.jsonassert.comparator.CustomComparator

示例:

compareJSON(expected, actual,  new CustomComparator(JSONCompareMode,new Customization(addressList.street,(o1, o2) -> true)))