samedi 3 juillet 2021

How to return object in dynamic nested json response

Issue Description

Hi I'm try to extract object in dynamic nested json response ,below method works without return but I need get object value to store it as array list for later usage now I getting null value please help me to get solve thanks in advance

    public static Object getKey(JSONObject jsonObject, String key) {
        boolean exists = jsonObject.has(key);
        Iterator<String> keys;
        String nextKeys;
        Object value = null;

        if (!exists) {
            keys = jsonObject.keys();
            while (keys.hasNext()) {
                nextKeys = keys.next();
                try {
                    if (jsonObject.get(nextKeys) instanceof JSONObject) {
                        if (exists == false) {
                            getKey(jsonObject.getJSONObject(nextKeys), key);
                        }
                    } else if (jsonObject.get(nextKeys) instanceof JSONArray) {
                        JSONArray jsonArray = jsonObject.getJSONArray(nextKeys);
                        for (int i = 0; i < jsonArray.length(); i++) {
                            String jsonArrayString = jsonArray.get(i).toString();
                            JSONObject innerJson = new JSONObject(jsonArrayString);
                            if (exists == false) {
                                getKey(innerJson, key);
                            }
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        } else {
            value = jsonObject.get(key);
        }
        return value;
    }

Aucun commentaire:

Enregistrer un commentaire