如何将JSON响应转换为Java List-使用Rest Assured进行API测试

问题描述 投票:0回答:2

我有一个嵌套的JSON响应。 JsonResponse Screenshot

我想从列表中的第0个位置获取字典,然后从中获取特定元素。对于例如作为响应,{0}和{1},我希望得到完整的{0}。然后从{0}开始,我只想提取“Id”值。 我不想每次都使用JsonPath.read(JsonResponse String,JSON Path)。所以寻找一些简单而更好的选择。

如何将JSON响应转换为Java List。以下是回复。

Response resp = given().header("Authorization", "Bearer "+"dwded").
                accept(ContentType.JSON).
                when().
                get("https://example.com");      
                return resp;
java json automation rest-assured rest-assured-jsonpath
2个回答
0
投票

为了测试web-API或REST端点,我建议使用Karate

所以变得简单:

* def id = response[0].Id

0
投票

在招摇的编辑宠物例子中。

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

从中生成模型

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

这生成了一个java类

public class Pet   {

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

api显示一个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);
© www.soinside.com 2019 - 2024. All rights reserved.