Conversión de JSONarray a ArrayList

Estoy descargando una cadena JSON y convirtiéndola en JSONArray. Lo estoy poniendo en una lista y necesito poder borrarlo de esa lista más adelante, y dado que JSONArray no tiene un método .remove (Gracias a Obama), estoy tratando de convertirlo a una lista de arrays.

aquí está mi JSON (el array.toString ()):

[{"thumb_url":"tb-1370913834.jpg","event_id":"15","count":"44","event_tagline":"this is a tagline","event_name":"5th birthday","event_end":"1370919600","event_start":"1370876400"}] 

Necesito ponerlo en una matriz y ser capaz de llamar a las cadenas con sus respectivas teclas. Apreciar cualquier ayuda!

 ArrayList listdata = new ArrayList(); JSONArray jArray = (JSONArray)jsonObject; if (jArray != null) { for (int i=0;i 

Lo hice usando Gson (por Google) .

Agregue la siguiente línea a build.gradle su módulo:

 dependencies { // ... // Note that `compile` will be deprecated. Use `implementation` instead. // See https://stackoverflow.com/a/44409111 for more info implementation 'com.google.code.gson:gson:2.8.2' } 

Cadena JSON :

 private String jsonString = "[\n" + " {\n" + " \"id\": \"c200\",\n" + " \"name\": \"Ravi Tamada\",\n" + " \"email\": \"ravi@gmail.com\",\n" + " \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" + " \"gender\" : \"male\",\n" + " \"phone\": {\n" + " \"mobile\": \"+91 0000000000\",\n" + " \"home\": \"00 000000\",\n" + " \"office\": \"00 000000\"\n" + " }\n" + " },\n" + " {\n" + " \"id\": \"c201\",\n" + " \"name\": \"Johnny Depp\",\n" + " \"email\": \"johnny_depp@gmail.com\",\n" + " \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" + " \"gender\" : \"male\",\n" + " \"phone\": {\n" + " \"mobile\": \"+91 0000000000\",\n" + " \"home\": \"00 000000\",\n" + " \"office\": \"00 000000\"\n" + " }\n" + " },\n" + " {\n" + " \"id\": \"c202\",\n" + " \"name\": \"Leonardo Dicaprio\",\n" + " \"email\": \"leonardo_dicaprio@gmail.com\",\n" + " \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" + " \"gender\" : \"male\",\n" + " \"phone\": {\n" + " \"mobile\": \"+91 0000000000\",\n" + " \"home\": \"00 000000\",\n" + " \"office\": \"00 000000\"\n" + " }\n" + " },\n" + " {\n" + " \"id\": \"c203\",\n" + " \"name\": \"John Wayne\",\n" + " \"email\": \"john_wayne@gmail.com\",\n" + " \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" + " \"gender\" : \"male\",\n" + " \"phone\": {\n" + " \"mobile\": \"+91 0000000000\",\n" + " \"home\": \"00 000000\",\n" + " \"office\": \"00 000000\"\n" + " }\n" + " },\n" + " {\n" + " \"id\": \"c204\",\n" + " \"name\": \"Angelina Jolie\",\n" + " \"email\": \"angelina_jolie@gmail.com\",\n" + " \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" + " \"gender\" : \"female\",\n" + " \"phone\": {\n" + " \"mobile\": \"+91 0000000000\",\n" + " \"home\": \"00 000000\",\n" + " \"office\": \"00 000000\"\n" + " }\n" + " },\n" + " {\n" + " \"id\": \"c205\",\n" + " \"name\": \"Dido\",\n" + " \"email\": \"dido@gmail.com\",\n" + " \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" + " \"gender\" : \"female\",\n" + " \"phone\": {\n" + " \"mobile\": \"+91 0000000000\",\n" + " \"home\": \"00 000000\",\n" + " \"office\": \"00 000000\"\n" + " }\n" + " },\n" + " {\n" + " \"id\": \"c206\",\n" + " \"name\": \"Adele\",\n" + " \"email\": \"adele@gmail.com\",\n" + " \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" + " \"gender\" : \"female\",\n" + " \"phone\": {\n" + " \"mobile\": \"+91 0000000000\",\n" + " \"home\": \"00 000000\",\n" + " \"office\": \"00 000000\"\n" + " }\n" + " },\n" + " {\n" + " \"id\": \"c207\",\n" + " \"name\": \"Hugh Jackman\",\n" + " \"email\": \"hugh_jackman@gmail.com\",\n" + " \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" + " \"gender\" : \"male\",\n" + " \"phone\": {\n" + " \"mobile\": \"+91 0000000000\",\n" + " \"home\": \"00 000000\",\n" + " \"office\": \"00 000000\"\n" + " }\n" + " },\n" + " {\n" + " \"id\": \"c208\",\n" + " \"name\": \"Will Smith\",\n" + " \"email\": \"will_smith@gmail.com\",\n" + " \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" + " \"gender\" : \"male\",\n" + " \"phone\": {\n" + " \"mobile\": \"+91 0000000000\",\n" + " \"home\": \"00 000000\",\n" + " \"office\": \"00 000000\"\n" + " }\n" + " },\n" + " {\n" + " \"id\": \"c209\",\n" + " \"name\": \"Clint Eastwood\",\n" + " \"email\": \"clint_eastwood@gmail.com\",\n" + " \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" + " \"gender\" : \"male\",\n" + " \"phone\": {\n" + " \"mobile\": \"+91 0000000000\",\n" + " \"home\": \"00 000000\",\n" + " \"office\": \"00 000000\"\n" + " }\n" + " },\n" + " {\n" + " \"id\": \"c2010\",\n" + " \"name\": \"Barack Obama\",\n" + " \"email\": \"barack_obama@gmail.com\",\n" + " \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" + " \"gender\" : \"male\",\n" + " \"phone\": {\n" + " \"mobile\": \"+91 0000000000\",\n" + " \"home\": \"00 000000\",\n" + " \"office\": \"00 000000\"\n" + " }\n" + " },\n" + " {\n" + " \"id\": \"c2011\",\n" + " \"name\": \"Kate Winslet\",\n" + " \"email\": \"kate_winslet@gmail.com\",\n" + " \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" + " \"gender\" : \"female\",\n" + " \"phone\": {\n" + " \"mobile\": \"+91 0000000000\",\n" + " \"home\": \"00 000000\",\n" + " \"office\": \"00 000000\"\n" + " }\n" + " },\n" + " {\n" + " \"id\": \"c2012\",\n" + " \"name\": \"Eminem\",\n" + " \"email\": \"eminem@gmail.com\",\n" + " \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" + " \"gender\" : \"male\",\n" + " \"phone\": {\n" + " \"mobile\": \"+91 0000000000\",\n" + " \"home\": \"00 000000\",\n" + " \"office\": \"00 000000\"\n" + " }\n" + " }\n" + " ]"; 

ContactModel.java :

 public class ContactModel { public String id; public String name; public String email; } 

Código para convertir una cadena JSON en ArrayList :

Nota: debe importar java.lang.reflect.Type; :

 // Top of file import java.lang.reflect.Type; // ... private void parseJSON() { Gson gson = new Gson(); Type type = new TypeToken>(){}.getType(); List contactList = gson.fromJson(jsonString, type); for (ContactModel contact : contactList){ Log.i("Contact Details", contact.id + "-" + contact.name + "-" + contact.email); } } 

Espero que esto te ayudará.

Tengo una solución rápida. Solo crea un archivo ArrayUtil.java

 import java.util.ArrayList; import java.util.Collection; import org.json.JSONArray; import org.json.JSONException; public class ArrayUtil { public static ArrayList convert(JSONArray jArr) { ArrayList list = new ArrayList(); try { for (int i=0, l=jArr.length(); i list) { return new JSONArray(list); } } 

Uso:

 ArrayList list = ArrayUtil.convert(jArray); 

o

 JSONArray jArr = ArrayUtil.convert(list); 

intente de esta manera Simplemente recorra eso, construyendo su propia matriz. Este código supone que es una matriz de cadenas, no debería ser difícil modificarla para adaptarse a su estructura de matriz particular.

 JSONArray jsonArray = new JSONArray(jsonArrayString); List list = new ArrayList(); for (int i=0; i 

En lugar de convertir la cadena JSON a una ArrayList o incluso a un Mapa, puede crear un JSONObject . Este objeto tiene la capacidad de obtener valores de cadena por clave , como desee y también para eliminar objetos .

Para crear un JSONObject partir de una cadena JSON correctamente formateada, simplemente llame al constructor apropiado .

 JSONObject json = new JSONObject(jsonString); 
  JSONArray array = new JSONArray(json); List list = new ArrayList(); for (int i = 0; i < array.length();list.add(array.getJSONObject(i++))); 

Para que sea práctico, usa POJO.

intenta así …

 List yourPojos = new ArrayList(); JSONObject jsonObject = new JSONObject(jsonString); YourPojoObject yourPojo = new YourPojoObject(); yourPojo.setId(jsonObject.getString("idName")); ... ... yourPojos.add(yourPojo); 

En Java 8,

 IntStream.range(0,jsonArray.length()).mapToObj(i->jsonArray.getString(i)).collect(Collectors.toList()) 
 ArrayList dataList = new ArrayList(); JSONArray jsonArray = (JSONArray)jsonObject; for(Object obj : jsonArray){ dataList.add((String)obj); } 
 public static List getJSONObjectListFromJSONArray(JSONArray array) throws JSONException { ArrayList jsonObjects = new ArrayList<>(); for (int i = 0; i < (array != null ? array.length() : 0); jsonObjects.add(array.getJSONObject(i++)) ); return jsonObjects; }