我的 API 响应:
{
"123": {
"userId": 424,
"firstName": "abc",
"lastName": "xyz",
"username": "abc",
"email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c6a7a4a586a1aba7afaae8a5a9ab" rel="noreferrer noopener nofollow">[email protected]</a>",
"status": 1
},
"234": {
"userId": 937,
"firstName": "xyz",
"lastName": "abc",
"username": "xyz",
"email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="97efeeedd7faf6fefbfef9f6e3f8e5b9f4f8fa" rel="noreferrer noopener nofollow">[email protected]</a>",
"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\": \"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a6c7c4c5e6c1cbc7cfca88c5c9cb" rel="noreferrer noopener nofollow">[email protected]</a>\",\n \"status\": 1\n },\n \"234\": {\n \"userId\": 937,\n \"firstName\": \"xyz\",\n \"lastName\": \"abc\",\n \"username\": \"xyz\",\n \"email\": \"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="90e8e9ead0fdf1f9fcf9fef1e4ffe2bef3fffd" rel="noreferrer noopener nofollow">[email protected]</a>\",\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));
}
关于java - 如何在放心框架中使用索引从 jsonpath 获取第一个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55115907/