簡體   English   中英

如何使用放心框架中的索引從jsonpath中獲取第一個元素?

[英]How to get first element from jsonpath using index in rest-assured framework?

我的 API 響應:

{
    "123": {
        "userId": 424,
        "firstName": "abc",
        "lastName": "xyz",
        "username": "abc",
        "email": "abc@gmail.com",
        "status": 1
    },
    "234": {
        "userId": 937,
        "firstName": "xyz",
        "lastName": "abc",
        "username": "xyz",
        "email": "xyz@mailinator.com",
        "status": 0
    },
    and so on ..
}

我的代碼是這樣的:

import groovy.inspect.swingui.GeneratedBytecodeAwareGroovyClassLoader;
import io.restassured.RestAssured;
import io.restassured.http.Method;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

@Test
public void getUserIdTest() throws IOException, ParseException, SQLException {

String baseUrl = readPropertiesFile().getProperty("baseUrl");
        RestAssured.baseURI = baseUrl;
        RequestSpecification httpRequest = RestAssured.given();
        Response response = httpRequest.request(Method.GET, "myApiPath");
        JsonPath jsonPathEvaluator = response.getBody().jsonPath();

// Now after this, I want to get the value of the userId in the first nested json. I can't use the string "123" e.g. jsonPathEvaluator.get("123.userId") since it is dynamic in nature.
}

請幫助我使用索引或任何其他方式在第一個嵌套的 json 中找到 userId。 提前致謝!

我嘗試過但在 RestAssured JsonPath 庫中沒有運氣。

或者,我使用 org.json 庫將 json 字符串解析為 JSONObject 和 Iterator 以獲取第一個元素的索引。 下面的代碼片段使用您提供的 json 字符串進行了測試。

import groovy.inspect.swingui.GeneratedBytecodeAwareGroovyClassLoader;
import io.restassured.RestAssured;
import io.restassured.http.Method;
import io.restassured.path.json.JsonPath;
import org.json.JSONObject;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.testng.annotations.Test;

import junit.framework.Assert;

@Test
public void getUserIdTest() throws IOException, ParseException, SQLException {

        String baseUrl = readPropertiesFile().getProperty("baseUrl");
        RestAssured.baseURI = baseUrl;
        RequestSpecification httpRequest = RestAssured.given();
        Response response = httpRequest.request(Method.GET, "myApiPath");
        String jsonResponseString = response.getBody().asString();
        //String jsonResponseString = "{\n    \"123\": {\n        \"userId\": 424,\n        \"firstName\": \"abc\",\n        \"lastName\": \"xyz\",\n        \"username\": \"abc\",\n        \"email\": \"abc@gmail.com\",\n        \"status\": 1\n    },\n    \"234\": {\n        \"userId\": 937,\n        \"firstName\": \"xyz\",\n        \"lastName\": \"abc\",\n        \"username\": \"xyz\",\n        \"email\": \"xyz@mailinator.com\",\n        \"status\": 0\n    }\n}";
        JSONObject jsonObject = new JSONObject(jsonResponseString);
        Iterator<String> keys = jsonObject.keys();

        String firstkey =keys.next(); 

        JSONObject jsonObjectElement = new JSONObject( jsonObject.get(firstkey).toString());
        String userId = jsonObjectElement.get("userId").toString();
        Assert.assertEquals(424, Integer.parseInt(userId));

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM