Java 如何在rest-assured框架中使用索引从jsonpath获取第一个元素?

Java 如何在rest-assured框架中使用索引从jsonpath获取第一个元素?,java,rest-assured,jsonpath,rest-assured-jsonpath,Java,Rest Assured,Jsonpath,Rest Assured Jsonpath,我的api回应: { "123": { "userId": 424, "firstName": "abc", "lastName": "xyz", "username": "abc", "email": "abc@gmail.com", "status": 1 }, "234": { "userId": 937, "firstName": "xy

我的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中查找用户ID。提前谢谢

我尝试过,但在重新启动的JsonPath库中没有运气

或者,我使用org.json库将json字符串解析为JSONObject,并使用迭代器获取第一个元素的索引。下面的代码片段使用您提供的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));

}
导入groovy.inspect.swingui.generatedByteCodeAwaRegroovCyclassLoader;
导入io.restassured.restassured;
导入io.restasured.http.Method;
导入io.restassured.path.json.JsonPath;
导入org.json.JSONObject;
导入io.restassured.response.response;
导入io.restassured.specification.RequestSpecification;
导入org.testng.annotations.Test;
导入junit.framework.Assert;
@试验
public void getUserIdTest()引发IOException、ParseException、SQLException{
字符串baseUrl=readPropertiesFile().getProperty(“baseUrl”);
RestAssured.baseURI=baseUrl;
RequestSpecification httpRequest=RestAssured.given();
Response-Response=httpRequest.request(Method.GET,“myApiPath”);
字符串jsonResponseString=response.getBody().asString();
//字符串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\”用户名\“:\”xyz\“,\n\”电子邮件\“:\”xyz@mailinator.com\“,\n\“status\”:0\n}\n}”;
JSONObject JSONObject=新的JSONObject(jsonResponseString);
迭代器keys=jsonObject.keys();
String firstkey=keys.next();
JSONObject jsonObjectElement=newjsonobject(JSONObject.get(firstkey.toString());
字符串userId=jsonObjectElement.get(“userId”).toString();
Assert.assertEquals(424,Integer.parseInt(userId));
}
使用jsonPathEvaluator.get(“$.*.userId”);引发错误java.lang.IllegalArgumentException:无效的JSON表达式:Script1.groovy:1:意外标记:。@第1行,第29列。$…userId ^1错误