Android: Dynamically Get JSON Array Key Name From JSON
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.???
5 Answers
5
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)
@IndependentDev You are welcome, and glad I could help you :)
– Muhammed Refaat
Mar 8 '16 at 11:54
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");
I dont need this method. I want to dynamically get the array names(for eg: All_details) and using this I want to dynamically get the values inside that array.
– IndependentDev
Mar 8 '16 at 11:36
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.
response
JSONArray
JSONObject
JSONArray
getJSONArray(String)
JSONException
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)
JSONArray
First of all I want to inform you that it's not a valid JSON. Remove the last Comma (,) to make it valid.
JSON
Then you can Iterate like here
JSONArray myKeys = response.names();
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();
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thank a lot... Its working...
– IndependentDev
Mar 8 '16 at 11:53