Java 从API响应的json数组中提取值,无括号不起作用

Java 从API响应的json数组中提取值,无括号不起作用,java,json,rest-assured,web-api-testing,rest-assured-jsonpath,Java,Json,Rest Assured,Web Api Testing,Rest Assured Jsonpath,以下是JSON: { "first":0, "rows":100, "data":[ { "id":326, "tag":"QATA9", "workNo":"qat12345" } ], "totalRecords":1 } 我的代码是: JsonPath jsonPathEvaluator = response.jsonPath(); wID = jsonPathEvaluator.get("data.id"); System.out.println("id is "+ wID); St

以下是JSON:

{
"first":0,
"rows":100,
"data":[
{
"id":326,
"tag":"QATA9",
"workNo":"qat12345"
}
],
"totalRecords":1
}
我的代码是:

JsonPath jsonPathEvaluator = response.jsonPath();
wID = jsonPathEvaluator.get("data.id");
System.out.println("id is "+ wID);

String responseBody = response.getBody().asString();
int statusCode = response.getStatusCode();
它在输出中显示 [326]
但是我只需要值326

这个
[]
定义了一个数组,所以库将它视为一个数组。只要选择第一个元素,你就可以了

试试这个:

JsonPath jsonPathEvaluator = response.jsonPath();
wID = jsonPathEvaluator.get("data.id")[0];
System.out.println("id is "+ wID);

然后,您还应该记住,首先使用数组的事实可能表明您可能有多个元素;在这种情况下,您只需在数组中循环。

这个
[]
定义了一个数组,因此库将其视为一个数组。只要选择第一个元素,你就可以了

试试这个:

JsonPath jsonPathEvaluator = response.jsonPath();
wID = jsonPathEvaluator.get("data.id")[0];
System.out.println("id is "+ wID);
然后,您还应该记住,首先使用数组的事实可能表明您可能有多个元素;在这种情况下,您只需在数组中循环。

试试这个

 JsonPath jsonPathEvaluator = response.jsonPath();
wID = jsonPathEvaluator.get("data[0].id");
System.out.println("id is "+ wID);
试试这个

 JsonPath jsonPathEvaluator = response.jsonPath();
wID = jsonPathEvaluator.get("data[0].id");
System.out.println("id is "+ wID);