【发布时间】:2019-03-12 07:11:21
【问题描述】:
我的 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。提前致谢!
【问题讨论】:
-
使用 jsonPathEvaluator.get("$.*.userId");抛出错误 java.lang.IllegalArgumentException:无效的 JSON 表达式:Script1.groovy:1:意外令牌:。 @ 第 1 行,第 29 列。$..userId ^ 1 错误
标签: java rest-assured jsonpath rest-assured-jsonpath