Skip to content

Instantly share code, notes, and snippets.

@rastaman
Created April 7, 2016 22:56
Show Gist options
  • Select an option

  • Save rastaman/c388707b834e66ba2e6709d8b67a91f1 to your computer and use it in GitHub Desktop.

Select an option

Save rastaman/c388707b834e66ba2e6709d8b67a91f1 to your computer and use it in GitHub Desktop.
Why no Iterable JSON Array in org.json ?
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.json.JSONArray;
public class IterableJSONArray<T> extends JSONArray implements Iterable<T> {
@SuppressWarnings("unchecked")
@Override
public Iterator<T> iterator() {
List<T> result = new ArrayList<T>();
for (int i = 0; i < this.length(); i++) {
result.add((T) this.get(i));
}
return result.iterator();
}
}
@rastaman
Copy link
Author

rastaman commented Apr 7, 2016

Note : there is much more efficient ways to write the iterable method, it is just an example/POC.

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