[英]extract value from json array of API response without brackets not working
Here is JSON: 这是JSON:
{
"first":0,
"rows":100,
"data":[
{
"id":326,
"tag":"QATA9",
"workNo":"qat12345"
}
],
"totalRecords":1
}
And my code is : 我的代码是:
JsonPath jsonPathEvaluator = response.jsonPath();
wID = jsonPathEvaluator.get("data.id");
System.out.println("id is "+ wID);
String responseBody = response.getBody().asString();
int statusCode = response.getStatusCode();
In output it shows [326] But i need value only 326 在输出中它显示[326]但我只需要值326
The []
delimits an array, so the library is treating it as an array. []
分隔数组,因此库将其视为数组。 Just pick the first element, and you should be fine. 只需选择第一个元素,就可以了。
Try this: 尝试这个:
JsonPath jsonPathEvaluator = response.jsonPath();
wID = jsonPathEvaluator.get("data.id")[0];
System.out.println("id is "+ wID);
Then, again, you should also have in mind that, the fact that an array was used in the first place may indicate that you may have more than one element; 然后,再次,您还应该记住,首先使用数组这一事实可能表明您可能有多个元素。 in that case, you should simply loop through the array.
在这种情况下,您应该只遍历整个数组。
try this 尝试这个
JsonPath jsonPathEvaluator = response.jsonPath();
wID = jsonPathEvaluator.get("data[0].id");
System.out.println("id is "+ wID);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.