Get key name jsonarray

I need to get the key name JsonArray, so JSON looks like this, please don't that JSON starts with square brackets and there are objects inside it that were made, I think, because the back end will have the opportunity to add objects.

[ { "tehnology": [ ] }, { "science": [] } ] 

So, I need to get the names “technology” and “science” from it, because json can change dynamically, how can I implement it?

+5
source share
1 answer

JSONArray contains JSONObject s. Extract each JSONObject and use keys() to access the key specified in each JSONObject

  JSONArray jArray = new JSONArray(response); for (int i = 0; i < jArray.length(); i++) { JSONObject object = jArray.optJSONObject(i); Iterator<String> iterator = object.keys(); while(iterator.hasNext()) { String currentKey = iterator.next(); } } 
+6
source

All Articles