Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't there a remove(int position) method in Android's JSONArray?

Tags:

json

android

I started building an Android app that uses a flat file for storage. The app doesn't store more than six records, and I'm familiar with JSON, so I just write out a JSONArray to the file.

I just discovered today, though, that the Android JSON API doesn't include a remove() option. Huh? Do I have to dump the array into another collection, remove it, then rebuild the JSONArray? What's the point?

like image 280
alalonde Avatar asked Sep 03 '10 19:09

alalonde


1 Answers

I use:

public static JSONArray removeFrom(JSONArray jsonArray, int pos){
    JSONArray result = new JSONArray();
    try {
        for (int i = 0; i < jsonArray.length(); i++) {
            if (i != pos) {
                jsonArray.put(jsonArray.get(i));
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}
like image 152
Fabio Guerra Avatar answered Oct 12 '22 01:10

Fabio Guerra