尝试从不同节点级别提取特定键的值时遇到错误

时间:2019-03-17 15:39:30

标签: java json

我正在尝试从所有不同的节点为avatar增值。我的json看起来像这样

{  
   "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"
   }
}

我尝试了以下解决方案

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());
    }

它不断给我以下错误

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)

1 个答案:

答案 0 :(得分:0)

如果不确定给定密钥后面的optJSONObject节点类型,则应使用JSON方法。如果给定值不是getJSONObject,则org.json.JSONException方法将引发JSONObject异常。您可以在下面找到固定方法的实现:

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);
                }
            }
        }
    }
}

List avatar-es返回URL很有用,因为对于每个URL avatar都是键。