Hi im wanting to create an array of all the keys in a JSONObject. my understanding (please correct me if i\'m wrong) is that i need to convert the JSONObject to a Map and th
Use the [keys()
][1] iterator to iterate over all the properties, and call [get()
][2] for each.
Iterator<String> iter = json.keys();
while (iter.hasNext()) {
String key = iter.next();
try {
Object value = json.get(key);
} catch (JSONException e) {
// Something went wrong!
}
}
No need to convert JSONObject
to a Map and then create an Array of keys just use JSONObject.names() for getting all keys in an JsonArray then convert it to Array or ArrayList. example:
JSONObject json = new JSONObject("json object string");
JSONArray namearray=json.names(); //<<< get all keys in JSONArray
Try this:
ArrayList<String> list = new ArrayList<String>();
JSONArray jsonArray = (JSONArray)jsonObject;
if (jsonArray != null) {
int len = jsonArray.length();
for (int i=0;i<len;i++){
list.add(jsonArray.get(i).toString());
}
}
String[] array = list.toArray(new String[list.size()]);