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

305
Visualizações
Get Key and values from JSONObject

I am trying to extract Key and values from my JSONObject. But i am not able to do that. Here is the JSON:

[{"id":["5"]},{"Tech":["Java"]}]

It is a String initially. I have converted that to JSONObject using :

JSONObject jsonObj = new JSONObject("[{"id":["5"]},{"Tech":["Java"]}]");

Then i am trying to get the key and value by:

jsonObj.getString("id");

But its giving me null. Can anyone help me out here?

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Try this:

try {
    JSONArray jsonArr = new JSONArray("[{\"id\":[\"5\"]},{\"Tech\":[\"Java\"]}]");

    for (int i = 0; i < jsonArr.length(); i++) {
        JSONObject jsonObj = jsonArr.getJSONObject(i);
        String k = jsonObj.keys().next();
        Log.i("Info", "Key: " + k + ", value: " + jsonObj.getString(k));
    }

} catch (JSONException ex) {
    ex.printStackTrace();
}
about 4 years ago · Santiago Trujillo Relatório

0

Parameter you are sending is JsonArray and referring to JsonObject. The Correct way is

JSONObject jsonObj = new JSONObject("{'id':['5','6']},{'Tech':['Java']}");      
    System.out.println(jsonObj.getString("id"));

    JSONArray jsonArray = new JSONArray("[{'id':['5','6','7','8']},{'Tech':['Java']}]");
    System.out.println(jsonArray.length());
    for(int i=0;i<jsonArray.length();i++){
            System.out.println(jsonArray.getJSONObject(i).getString("id"));
    }
about 4 years ago · Santiago Trujillo Relatório

0

Because at id you dont have a string , you have a array of string ,

so insted of doing this jsonObj.getString("id");

do this

jsonObj.getArray("id"); this will give you that array in return

like if you have a Json Array at id then you have to do this

jsonObj.getJSONArray("id");

about 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