簡體   English   中英

如何將JSON響應轉換為Java列表-使用Rest Assure確保進行API測試

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

我有一個嵌套的JSON響應。 JsonResponse屏幕截圖

我想從列表中的第0個位置獲取字典,然后從中獲取一個特定的元素。 例如,作為響應{0}和{1},我希望得到完整的{0}。 然后從{0}開始,我只想提取“ Id”值。
我不想每次都使用JsonPath.read(JsonResponse String,JSON Path) 因此,正在尋找一些更簡單,更好的選擇。

如何將JSON響應轉換為Java列表。 以下是響應。

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

為了測試Web-API或REST端點,我建議Karate

這樣就變得簡單了:

* def id = response[0].Id

在招搖過頭的編輯器寵物示例中。

  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,該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