Parse JSON Array sin clave en Android
¿Cómo analizaría una matriz como la siguiente en Android?
[ 5, 10, 15, 20 ]
Como puede ver, no hay ninguna clave que defina la matriz, como otras matrices ejemplo tienen, como esto:
- ¿Cómo realizar pruebas sin conexión para aplicaciones que dependen de JSON?
- Actualizar elementos en un objeto JSONO
- HttpGet con el cuerpo de la solicitud Android
- Publicar campos en este formato con Retrofit
- JSONObject to String Android
{ "items": [ 5, 10, 15 ] }
Para la segunda matriz, puedo hacer fácilmente un JSONObject
y sólo uso:
JSONArray itemArray = jsonObject.getJSONArray("items")
Pero, como es obvio, no hay clave para la primera matriz. Entonces, ¿cómo se puede hacer esto? ¿Es posible incluso con bibliotecas estándar de Android?
- Acceder a la respuesta JSON desde la vista web de android
- Conversión de cadena a json objeto android
- Cargar imagen en ImageView de la URL almacenada en JSONString en Android
- Volley JSONException: Fin de la entrada en el carácter 0 de
- Java / Android: java.lang.OutOfMemoryError mientras se construye un objeto JSON
- Cómo convertir JSONArray a ListView?
- ConstraintLayout forma de contenido cambiado si se desplaza
- Cómo analizar la matriz json anidado en android utilizando volley biblioteca
¿Has intentado hacer esto?
try { // jsonString is a string variable that holds the JSON JSONArray itemArray=new JSONArray(jsonString); for (int i = 0; i < itemArray.length(); i++) { int value=itemArray.getInt(i); Log.e("json", i+"="+value); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); }
Considerar la versión de Foreach:
try { JSONArray itemArray=jsonObject.getJSONArray("items"); for (var item : itemArray) { System.out.println(item); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); }
String wrappedArray = "{'items':" + justAnArray + "}";
Luego analiza como solías.
Puede pasar la cadena de matriz al constructor JSONArray
la JSONArray
manera:
String itemsJson = "[5,10,15,20]"; JSONArray itemArray = new JSONArray(itemsJson);
Nota: tendrá que agregar un try/catch
o throws
para el JSONException
potencial
Aquí puede acceder directamente a los datos en json array.
JSONArray itemArray = jsonObject.getJSONArray("items"); for(int i=0;i<itemarray.length;i++) { int i = Integer.ParseInt(itemarray.get(i)); Log.i("Value is:::",""+i); }
- ¿Qué authTokenType significa exactamente?
- Configuración de AVD a las mismas especificaciones que el dispositivo físico