0

这是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

4

2 回答 2

2

[]分隔数组,因此库将其视为数组。只需选择第一个元素,就可以了。

尝试这个:

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

然后,您还应该记住,首先使用数组这一事实可能表明您可能有多个元素;在这种情况下,您应该简单地遍历数组。

于 2018-03-16T11:23:07.313 回答
0

尝试这个

 JsonPath jsonPathEvaluator = response.jsonPath();
wID = jsonPathEvaluator.get("data[0].id");
System.out.println("id is "+ wID);
于 2018-03-16T11:29:19.827 回答