Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

168
Visualizações
¿Cómo iterar sobre un JSONObject?

Uso una biblioteca JSON llamada JSONObject (no me importa cambiar si es necesario).

Sé cómo iterar sobre JSONArrays , pero cuando analizo datos JSON de Facebook no obtengo una matriz, solo un JSONObject , pero necesito poder acceder a un elemento a través de su índice, como JSONObject[0] para obtener el primero, y no sé cómo hacerlo.

 { "http://http://url.com/": { "id": "http://http://url.com//" }, "http://url2.co/": { "id": "http://url2.com//", "shares": 16 } , "http://url3.com/": { "id": "http://url3.com//", "shares": 16 } }
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Tal vez esto ayude:

 JSONObject jsonObject = new JSONObject(contents.trim()); Iterator<String> keys = jsonObject.keys(); while(keys.hasNext()) { String key = keys.next(); if (jsonObject.get(key) instanceof JSONObject) { // do something with jsonObject here } }
over 4 years ago · Santiago Trujillo Relatório

0

para mi caso, encontré que iterar los names() funciona bien

 for(int i = 0; i<jobject.names().length(); i++){ Log.v(TAG, "key = " + jobject.names().getString(i) + " value = " + jobject.get(jobject.names().getString(i))); }
over 4 years ago · Santiago Trujillo Relatório

0

Evitaré el iterador ya que pueden agregar/eliminar objetos durante la iteración, también para el uso de código limpio para el bucle. será simplemente limpio y con menos líneas.

Uso de Java 8 y Lamda [Actualización 2/4/2019]

 import org.json.JSONObject; public static void printJsonObject(JSONObject jsonObj) { jsonObj.keySet().forEach(keyStr -> { Object keyvalue = jsonObj.get(keyStr); System.out.println("key: "+ keyStr + " value: " + keyvalue); //for nested objects iteration if required //if (keyvalue instanceof JSONObject) // printJsonObject((JSONObject)keyvalue); }); }

Usando la forma antigua [Actualización 4/2/2019]

 import org.json.JSONObject; public static void printJsonObject(JSONObject jsonObj) { for (String keyStr : jsonObj.keySet()) { Object keyvalue = jsonObj.get(keyStr); //Print key and value System.out.println("key: "+ keyStr + " value: " + keyvalue); //for nested objects iteration if required //if (keyvalue instanceof JSONObject) // printJsonObject((JSONObject)keyvalue); } }

Respuesta Original

 import org.json.simple.JSONObject; public static void printJsonObject(JSONObject jsonObj) { for (Object key : jsonObj.keySet()) { //based on you key types String keyStr = (String)key; Object keyvalue = jsonObj.get(keyStr); //Print key and value System.out.println("key: "+ keyStr + " value: " + keyvalue); //for nested objects iteration if required if (keyvalue instanceof JSONObject) printJsonObject((JSONObject)keyvalue); } }
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda