Encipherer
Encipherer

Reputation: 421

Android: Dynamically Get JSON Array Key Name From JSON

I have a json link, if we open it I get a following result

{
"Status": "Success",

"All_Details": [{
    "Types": "0",
    "TotalPoints": "0",
    "ExpiringToday": 0
}],
"First": [{
    "id": "0",
    "ImagePath": "http://first.example.png"
}],
"Second": [{
    "id": "2",
    "ImagePath": "http://second.example.png"
}],
"Third": [{
    "id": "3",
    "ImagePath": "http://third.example.png"
}],

}

What I need is, I want to dynamically get all the key names like status, All_details, First etc.

And I also want to get the data inside the All_details and First Array. I used following method

@Override
        public void onResponse(JSONObject response) throws JSONException {
            VolleyLog.d(TAG, "Home Central OnResponse: " + response);

            String statusStr = response.getString("Status");
            Log.d(TAG, "Status: " + statusStr);

            if (statusStr.equalsIgnoreCase("Success")) {
                Iterator iterator = response.keys();
                while (iterator.hasNext()) {
                    String key = (String)iterator.next();
                }
            }
        }

I get all the key names in get stored in the String key. But I am unable to open get the values inside the JSON array, for eg. I need to get the values inside first and second array using the String(Key). How can I do that.???

Upvotes: 0

Views: 9984

Answers (5)

Muhammed Refaat
Muhammed Refaat

Reputation: 9103

First, to get the keynames, you can easily iterate through the JSONObject itself as mentioned here:

Iterator<?> keys = response.keys();
while( keys.hasNext() ) {
    String key = (String)keys.next();
    if ( response.get(key) instanceof JSONObject ) {
        System.out.println(key); // do whatever you want with it
    }
}

Then, to get the values of the array:

    JSONArray arr = response.getJSONArray(key);
    JSONObject element;
    for(int i = 0; i < arr.length(); i++){
        element = arr.getJSONObject(i); // which for example will be Types,TotalPoints,ExpiringToday in the case of the first array(All_Details) 
    }

Upvotes: 10

Devishankar
Devishankar

Reputation: 229

Try this one

Iterator keys = jsonObject.keys();
    while (keys.hasNext()) {
        try {
            String dynamicKey = (String) keys.next();//Your dynamic key
            JSONObject item = jsonObject.getJSONObject(dynamicKey);//Your json object for that dynamic key
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

Upvotes: 0

Shree Krishna
Shree Krishna

Reputation: 8562

First of all I want to inform you that it's not a valid JSON. Remove the last Comma (,) to make it valid.

Then you can Iterate like here

JSONArray myKeys = response.names();

Upvotes: 0

l7r7
l7r7

Reputation: 1328

If you want to get the JSON array from the response JSONObject you can use the JSONArray class. JSONObject has a method to get a JSONArray: getJSONArray(String). Remember to catch the JSONException when trying this. This exception will be thrown if there is no key for example.

Your code could look like this (only the while loop):

while (iterator.hasNext()) {
    String key = (String)iterator.next();
    try {
        JSONArray array = response.getJSONArray(key);
        // do some stuff with the array content
    } catch(JSONException e) {
        // handle the exception.
    }
}

You can get the values from the array with the methods of JSONArray (see the documentation)

Upvotes: 1

Bhushan Bhangale
Bhushan Bhangale

Reputation: 10987

Something like this will allow you to iterate on array and individual fields once you have extracted the keys using what you have done. Instead of "Types" use the key variable you will create before this.

JSONArray allDetails = response.getJsonArray("All_Details")

for (int i = 0 ; i < allDetails.length(); i++) {
    JSONObject allDetail = allDetails.getJSONObject(i);
    allDetails.getString("Types");
}

Upvotes: 0

Related Questions