简体   繁体   English

如何将JSON响应转换为Java列表-使用Rest Assure确保进行API测试

[英]How to convert JSON response to Java List- Using Rest Assured for API Testing

I have a nested JSON response. 我有一个嵌套的JSON响应。 JsonResponse Screenshot JsonResponse屏幕截图

I want to get the the dictionary at 0th location from the list and then get a particular element from it. 我想从列表中的第0个位置获取字典,然后从中获取一个特定的元素。 For eg In response, {0} and {1}, I want to get complete {0} as a result. 例如,作为响应{0}和{1},我希望得到完整的{0}。 Then from {0}, I want to extract "Id" value only. 然后从{0}开始,我只想提取“ Id”值。
I don't want to use the JsonPath.read(JsonResponse String, JSON Path) everytime. 我不想每次都使用JsonPath.read(JsonResponse String,JSON Path) So looking for some simple and better alternative. 因此,正在寻找一些更简单,更好的选择。

How to convert JSON response to Java List. 如何将JSON响应转换为Java列表。 Below is the response. 以下是响应。

Response resp = given().header("Authorization", "Bearer "+"dwded").
                accept(ContentType.JSON).
                when().
                get("https://example.com");      
                return resp;

For testing a web-API or REST endpoint, I would recommend Karate . 为了测试Web-API或REST端点,我建议Karate

So it becomes simple: 这样就变得简单了:

* def id = response[0].Id

In the swagger editor pet example. 在招摇过头的编辑器宠物示例中。

  responses:
    200:
      description: "successful operation"
      schema:
        type: "array"
        items:
          $ref: "#/definitions/Pet"

A model is generated from the 模型是从

  Pet:
    type: "object"
    properties:
      name:
        type: "string"
        example: "doggie"

This generated a java class 这产生了一个java类

public class Pet   {

  @JsonProperty("name")
  private String name = null;

The api shows a REST that returns an entity that can be shown as an array of json objects 该api显示了一个REST,该REST返回一个可以显示为json对象数组的实体

    ResponseEntity<List<Pet>> findPetsByStatus( @NotNull@ApiParam(value = "Status values that need to be considered for filter", required = true, allowableValues = "available, pending, sold") @RequestParam(value = "status", required = true) List<String> status);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM