Skip to content

Instantly share code, notes, and snippets.

@emmgfx
Created May 2, 2014 07:49
Show Gist options
  • Save emmgfx/0f018b5acfa3fd72b3f6 to your computer and use it in GitHub Desktop.
Save emmgfx/0f018b5acfa3fd72b3f6 to your computer and use it in GitHub Desktop.
Android JSONArray remove before API 19
// Example of use: remove(i, savedProfiles);
public static JSONArray remove(final int idx, final JSONArray from) {
final List<JSONObject> objs = asList(from);
objs.remove(idx);
final JSONArray ja = new JSONArray();
for (final JSONObject obj : objs) {
ja.put(obj);
}
return ja;
}
public static List<JSONObject> asList(final JSONArray ja) {
final int len = ja.length();
final ArrayList<JSONObject> result = new ArrayList<JSONObject>(len);
for (int i = 0; i < len; i++) {
final JSONObject obj = ja.optJSONObject(i);
if (obj != null) {
result.add(obj);
}
}
return result;
}
@mdev88
Copy link

mdev88 commented Apr 15, 2015

Excellent, thank you very much!

@aravsingh
Copy link

in may code json array is not using this remove method instead is using the internal method
........what to do?

@ItzNotABug
Copy link

is this method reliable to run on UI thread?
or do I need to wrap this in an AsyncTask?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment