-2

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)
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 2
    Instead of just asking "How do I do X?" please show your efforts. JSON parsing is so much asked topic you can find lot of tutorials in internet. We cannot help you to parse your entire json but rather a question like "This is how I am parsing JSON and this is the issue I am facing" etc. – Pradeep Simha Mar 17 '19 at 15:41
  • 2
    Hey guys, I have updated the question. I have been looking the answer, but could not find an answer, thus posted. Any idea why is that error is happening? – Md. Samsul Kabir Mar 17 '19 at 15:53

1 Answers1

0

You should use optJSONObject method if you are not sure what kind of JSON node it is behind given key. getJSONObject method throws org.json.JSONException exception when given value is not JSONObject. Below you can find fixed method implementation:

void getAllParentChildNodeAsMap(JSONObject jObject, List<String> result) {
    for (String key : jObject.keySet()) {
        if (key.equals("avatar")) {
            result.add(jObject.getString(key));
            continue;
        }
        JSONObject value = jObject.optJSONObject(key);
        if (value != null) {
            getAllParentChildNodeAsMap(value, result);
            continue;
        }
        JSONArray array = jObject.optJSONArray(key);
        if (array != null) {
            for (int i = 0; i < array.length(); i++) {
                JSONObject item = array.optJSONObject(i);
                if (item != null) {
                    getAllParentChildNodeAsMap(item, result);
                }
            }
        }
    }
}

It is much useful to return List with avatar URL-es because for each URL avatar is a key.

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146