{
"random": {
"5756cd7a-4662-4428-9b09-dbff3080450a": {
"@class": "one",
"id": "5756cd7a-4662-4428-9b09-dbff3080450a"
},
"857ef5ee-af98-4f24-89fe-29bdbebde882": {
"@class": "two",
"id": "857ef5ee-af98-4f24-89fe-29bdbebde882"
}
}
}
This is my api 'response' which is in type JsonPath.
String random = response.getString("random");
I'm stuck after this as to how to extract the first random element.
From looking at the documentation, perhaps you could use JsonPath#getMap
to do the extraction, eg:
final Map<String, RandomObject> result = response.getMap("random", String.class, RandomObject.class);
Where RandomObject
would be something like:
public class RandomObject {
private String klass; // N.B. Not sure how to deal with @class naming
private String id;
// ... constructor/setters/getters etc...
}
Although, I'm not very familiar with this JsonPath
library, so it may not be that easy.
Assuming you have your json response as string in responseJsonString, the code below may solve your problem.
List<String> randomIDS = JsonPath.read(responseJsonString, "$.random..id");
Collections.reverse(randomIDS);
for (String randomID : randomIDS) {
StringBuilder stringBuilder = new StringBuilder("$.random.");
String randomClass = JsonPath.read(
responseJsonString,
stringBuilder.append(randomID).append(".@class").toString());
System.out.println(randomClass);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.