rest-assured的JsonPath使用方法总结

JsonPath对于解析Json格式的数据来说非常简单,比如有下面这样的Json数据:

 1 {"lotto":{
 2     "lottoId":5,
 3     "winning-numbers":[2,45,34,23,7,5,3],
 4     "winners":[{
 5       "winnerId":23,
 6       "numbers":[2,45,34,23,3,5]
 7     },{
 8       "winnerId":54,
 9       "numbers":[52,3,12,11,18,22]
10     }]
11   }
12 }

下面是一些简单的使用实例:

 1 //这里的lottoJson代表了上面所说的json数据
 2 JsonPath jsonPath = new JsonPath(lottoJson);
 3 
 4 // 获取lottoId
 5 int lottoId = jsonPath.getInt("lotto.lottoId");
 6 
 7 // 获取winning-numbers列表
 8 List<string> winningNumbers = jsonPath.get("lotto.winning-numbers");
 9 
10 // 下面的语句会返回一个list,list中包含23,54
11 List<integer> winningNumbers = jsonPath.get("lotto.winners.winnerId");

从上面的例子中可以看到所有的获取路径中我们都重复使用了lotto,为了避免这个问题,我们可以设置一个根路径(root path):

 1  //这里lottoJson代表上面的json数据
 2  JsonPath jsonPath = new JsonPath(lottoJson);
 3  //设置根路径
 4  jsonPath.setRoot("lotto");
 5 
 6 // 获取lottoId
 7  int lottoId = jsonPath.getInt("lottoId");
 8 
 9 //获取winning-numbers列表
10 List<string> winningNumbers = jsonPath.get("winning-numbers");
11 
12 // 下面的语句将返回一个list,list中包含23,54
13 List<integer> winningNumbers = jsonPath.get("lotto.winners.winnerId");

如果你只是对提取一个单一的值感兴趣,你还可以这样做:

1 // "from"是从JsonPath中静态导入的
2 int lottoId = from(lottoJson).getInt("lotto.lottoId");

你也可以做一些复杂的操作,比如求winners.numbers的和:

1 int sumOfWinningNumbers = from(lottoJson).
2                            getInt("lotto.winning-numbers.sum()");

或者是找出所有大于10并且winnerId=23的number:

1 // 返回结果是包含45,34 and 23的list
2 List<integer> numbers = from(lottoJson).getList(
3   "lotto.winners.find {it.winnerId == 23}.numbers.findAll {it > 10}",
4    Integer.class);

 

转载于:https://www.cnblogs.com/lwjnicole/p/8269965.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当你在面试时,可能会被问到关于RestAssured的一些问题。以下是一些常见的RestAssured面试题: 1. 什么是RestAssuredRestAssured是一个用于测试RESTful API的开源Java库。它提供了一个简洁的DSL(领域特定语言)来编写易读易维护的API测试。 2. RestAssured的主要特点是什么? RestAssured具有以下主要特点: - 简化了处理HTTP请求和响应的过程。 - 提供了直观的方法和语法,使得编写API测试用例更加易于理解和维护。 - 具有丰富的验证和断言功能,可以验证API的响应是否满足预期。 - 支持多种身份认证和授权机制。 - 可以与其他测试框架(如TestNG和JUnit)集成。 3. 如何在项目中使用RestAssured? 你可以通过Maven或Gradle将RestAssured添加到你的项目依赖中。例如,在Maven项目中,你可以在pom.xml文件中添加以下依赖项: ```xml <dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <version>4.4.0</version> <scope>test</scope> </dependency> ``` 然后,你可以在你的测试类中导入RestAssured相关的类,开始编写API测试用例。 4. RestAssured中的基本用法是什么? 在RestAssured中,你可以使用given、when和then方法来编写API测试用例。例如,以下是一个使用RestAssured发送GET请求并验证响应的示例: ```java import static io.restassured.RestAssured.*; Response response = given() .baseUri("https://api.example.com") .basePath("/users") .when() .get("/1234") .then() .statusCode(200) .extract() .response(); ``` 在上面的例子中,我们首先使用given方法设置API请求的基本URL和路径,然后使用when方法指定HTTP方法和路径,最后使用then方法验证响应状态码是否为200,并提取响应对象。 这些是一些常见的RestAssured面试题及其答案。当然,在面试中可能会有更多特定的问题,但希望这些问题可以帮助你更好地理解和准备。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值