I am trying to extra value for avatar
from all different node. My json looks like this
{
"page":1,
"per_page":3,
"total":12,
"avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/olegpogodaev/128.jpg",
"total_pages":4,
"data":[
{
"id":1,
"first_name":"George",
"last_name":"Bluth",
"avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg"
},
{
"id":2,
"first_name":"Janet",
"last_name":"Weaver",
"avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg"
},
{
"id":3,
"first_name":"Emma",
"last_name":"Wong",
"avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/olegpogodaev/128.jpg"
}
],
"user":{
"id":3,
"first_name":"Emma",
"last_name":"Wong",
"avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/olegpogodaev/128.jpg"
}
}
I have tried following solution
public void getAllParentChildNodeAsMap(JSONObject jObject, Map<String, Object> result) throws JSONException {
Iterator<String> iterator = jObject.keys();
while (iterator.hasNext()) {
String key = (String) iterator.next();
Object value = null;
if (jObject.getJSONObject(key) instanceof JSONObject) {
JSONObject jsonValue = jObject.getJSONObject(key);
getAllParentChildNodeAsMap(jsonValue, result);
} else {
value = jObject.get(key);
}
if(key.equals("avatar")) {
result.put(key, value);
}
}
log.info(result.values().toString());
}
And it keeping giving me following error
org.json.JSONException: JSONObject["per_page"] is not a JSONObject.
at org.json.JSONObject.getJSONObject(JSONObject.java:782)
at com.rnd.restapi.serenity.steps.CommonFacilities.getAllParentChildNodeAsMap(CommonFacilities.java:72)
at com.rnd.restapi.serenity.steps.CommonFacilities$$EnhancerByCGLIB$$d9a1a28f.CGLIB$getAllParentChildNodeAsMap$5(<generated>)
at com.rnd.restapi.serenity.steps.CommonFacilities$$EnhancerByCGLIB$$d9a1a28f$$FastClassByCGLIB$$bb25cc09.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at net.thucydides.core.steps.BaseMethodRunner.invokeMethod(BaseMethodRunner.java:10)
at net.thucydides.core.steps.NormalMethodRunner.invokeMethodAndNotifyFailures(NormalMethodRunner.java:20)
at net.thucydides.core.steps.StepInterceptor.runNormalMethod(StepInterceptor.java:390)
at net.thucydides.core.steps.StepInterceptor.testStepResult(StepInterceptor.java:161)
at net.thucydides.core.steps.StepInterceptor.intercept(StepInterceptor.java:72)
at com.rnd.restapi.serenity.steps.CommonFacilities$$EnhancerByCGLIB$$d9a1a28f.getAllParentChildNodeAsMap(<generated>)
at com.rnd.restapi.serenity.steps.CommonFacilities.getAllParentChildNode(CommonFacilities.java:64)
at com.rnd.restapi.serenity.steps.CommonFacilities$$EnhancerByCGLIB$$d9a1a28f.CGLIB$getAllParentChildNode$4(<generated>)
at com.rnd.restapi.serenity.steps.CommonFacilities$$EnhancerByCGLIB$$d9a1a28f$$FastClassByCGLIB$$bb25cc09.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at net.thucydides.core.steps.StepInterceptor.invokeMethod(StepInterceptor.java:478)
at net.thucydides.core.steps.StepInterceptor.executeTestStepMethod(StepInterceptor.java:463)
at net.thucydides.core.steps.StepInterceptor.runTestStep(StepInterceptor.java:438)
at net.thucydides.core.steps.StepInterceptor.runOrSkipMethod(StepInterceptor.java:179)
at net.thucydides.core.steps.StepInterceptor.testStepResult(StepInterceptor.java:166)
at net.thucydides.core.steps.StepInterceptor.intercept(StepInterceptor.java:72)
at com.rnd.restapi.serenity.steps.CommonFacilities$$EnhancerByCGLIB$$d9a1a28f.getAllParentChildNode(<generated>)
at com.rnd.restapi.tests.CountriesSearchTests.get_service_is_successful_and_status_code(CountriesSearchTests.java:32)
at ✽.Get service is successful and status code 200(src/test/resource/feature/get.feature:7)