5

I need to get a key name of JsonArray, so JSON looks like this, please not, that JSON is starting with array brackets, and inside it it has objects, that was made i guess because back end will have the ability to add objects.

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

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

k.nadonenko
  • 628
  • 2
  • 7
  • 23

1 Answers1

9

The JSONArray contains JSONObjects. Retrieve every JSONObject and use keys() to access the key defined in every 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();
      }
 }
Blackbelt
  • 156,034
  • 29
  • 297
  • 305