栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

REST Assured 72 - How To Compare Part Of JSON Objects And Arrays Using JSONassert Library

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

REST Assured 72 - How To Compare Part Of JSON Objects And Arrays Using JSONassert Library

REST Assured 系列汇总 之 REST Assured 72 - How To Compare Part Of JSON Objects And Arrays Using JSonassert Library 介绍

很多时候我们只需比较特定某部分的 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 Objects

JSON 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,不然编译会出错。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/301583.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号