很多时候我们只需比较特定某部分的 JSON Objects 或 JSON Arrays。例如:假设我们创建一个新的订单并获取订单细节。当我们获取所有订单信息时,我们只需要验证刚创建的那个新的订单,不是验证所有的订单。
前提条件添加 JSonassert 依赖
org.skyscreamer jsonassert 1.5.0 test
请先阅读下面文章,了解 JSonassert Java library 基本概念 ,不同的比较模式和断言 JSON Objects 和 Array。
Introduction To JsonAssert Library
Compare JSON Objects Using JSonassert Library
Compare JSON Arrays Using JSonassert Library
比较部分 JSON ObjectsJSON Object 1
{
"lastName": "Animesh",
"firstName": "Prashant",
"address": {
"city": "Kolkata",
"state": "WB"
}
}
JSON Object 2
{
"lastName": "Animesh",
"firstName": "Prashant",
"communicationAddress": {
"city": "Kolkata",
"state": "WB"
},
"skills": [
"CA",
"BCOM"
]
}
我们需要比较第一个 JSON Object 中的 “address” object 和第二个 JSON Object 中的 “communicationAddress” object 的值是否相等。我们分别用 JsonPath 的方法来获取想要的部分 JSON Objects,然后用 assertEquals() 方法来比较。
import org.json.JSONException;
import org.json.JSONObject;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
public class ComparePortionOfJsonObjects {
public static void main(String[] args) throws JSONException {
JSONObject jsonObject1 = new JSONObject("{rn" +
" "lastName": "Animesh",rn" +
" "firstName": "Prashant",rn" +
" "address": {rn" +
" "city": "Kolkata",rn" +
" "state": "WB"rn" +
" }rn" +
"}");
JSONObject jsonObject2 = new JSONObject("{rn" +
" "lastName": "Animesh",rn" +
" "firstName": "Prashant",rn" +
" "communicationAddress": {rn" +
" "city": "Kolkata",rn" +
" "state": "WB"rn" +
" },rn" +
" "skills": [rn" +
" "CA",rn" +
" "BCOM"rn" +
" ]rn" +
"}");
// I want to assert address objects are same in both
// Since "address" and "communicationAddress" are JSON Objects so we need to use proper method i.e. getJSonObject()
JSONAssert.assertEquals(jsonObject1.getJSONObject("address"), jsonObject2.getJSONObject("communicationAddress"), JSONCompareMode.LENIENT);
}
}
比较部分 JSON Arrays
JSON Array 1
[
{
"id": 1,
"first_name": "Giavani",
"last_name": "Skellorne"
},
{
"id": 2,
"first_name": "Patton",
"last_name": "Buchett"
}
]
JSON Array 2
[
{
"id": 1,
"first_name": "Giavani",
"last_name": "Skellorne"
},
{
"id": 2,
"first_name": "Harland",
"last_name": "Brookwood"
},
{
"id": 3,
"first_name": "Leigh",
"last_name": "Gatteridge"
}
]
上两个 JSON Array,想比较比较数组中的第一个 objects
import org.json.JSONArray;
import org.json.JSONException;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
public class ComparePortionOfJsonArrays {
public static void main(String[] args) throws JSonException {
JSonArray jSONArray1 = new JSONArray("[rn" +
" {rn" +
" "id": 1,rn" +
" "first_name": "Giavani",rn" +
" "last_name": "Skellorne"rn" +
" },rn" +
" {rn" +
" "id": 2,rn" +
" "first_name": "Patton",rn" +
" "last_name": "Buchett"rn" +
" }rn" +
"]");
JSonArray jSONArray2 = new JSONArray("[rn" +
" {rn" +
" "id": 1,rn" +
" "first_name": "Giavani",rn" +
" "last_name": "Skellorne"rn" +
" },rn" +
" {rn" +
" "id": 2,rn" +
" "first_name": "Harland",rn" +
" "last_name": "Brookwood"rn" +
" },rn" +
" {rn" +
" "id": 3,rn" +
" "first_name": "Leigh",rn" +
" "last_name": "Gatteridge"rn" +
" }rn" +
"]");
// I want to assert address objects are same in both
// Since index elements in both arrays are JSON objects so using proper method i.e. getJSONObject
JSONAssert.assertEquals(jSONArray1.getJSONObject(0), jSONArray2.getJSONObject(0), JSONCompareMode.LENIENT);
}
}
你也可以比较 JSON Object 中的一部分和 JSON Array 中的一部分。但是要注意,比较的部分一定要是相同的 Type,不然编译会出错。



