How to decode a JSON Array on Android

3

There was a need to decode a JSON Array like this:

output: [ [{ }, { }], [{ }, { }] ]

I was doing this in a way that decoded a Array like this:

output: [{ }, { }, { }]

Code of how you were doing:

Method to convert Entity

private CardapioEntities convertCardapio(JSONObject obj) throws JSONException {
    String titulo = obj.getString(TAG_TITULO);
    String descricao = obj.getString(TAG_DESCRICAO);
    String preco = obj.getString(TAG_PRECO);
    String tipo = obj.getString(TAG_TIPO);

    return new CardapioEntities(titulo, descricao, preco, tipo);
}

Method to add to ListView

private void _getCardapio(String result) {
    //...
    try {
        JSONArray json = new JSONArray(result);
        for (int i = 0; i < json.length(); i++) {
                //...
                cardapioEntities.setCardapioPreco(convertCardapio(json.getJSONObject(i)).getCardapioPreco());
                listaLanches.add(cardapioEntities);
        }
        //...
        final ListView lvLanches = (ListView) findViewById(R.id.lvLanches);
        lvLanches.setAdapter(new CustomListAdapter(this, listaLanches));
        //...
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

Question:

How to adapt this code to read Json Array ? Exemplify.

ISSUE:

According to response from @ Wakim , another question arose: Why is the object repeating in ListView ? I think it's logic error rs.

private void _getCardapio(String result) {
    //...
    try {
        JSONArray json = new JSONArray(result);
        for (int i = 0; i < json.length(); i++) {
            CardapioEntities cardapioEntities = new CardapioEntities();
            JSONArray arrayDentroDoArray = json.getJSONArray(i);
            for (int j = 0; j < arrayDentroDoArray.length(); j++) {
                JSONObject objeto = arrayDentroDoArray.getJSONObject(j);

                if (convertCardapio(objeto).getCardapioTipo() == 0) {
                    cardapioEntities.setCardapioTitulo(convertCardapio(objeto).getCardapioTitulo());
                    cardapioEntities.setCardapioDescricao(convertCardapio(objeto).getCardapioDescricao());
                    cardapioEntities.setCardapioPreco(convertCardapio(objeto).getCardapioPreco());
                    listaLanches.add(cardapioEntities);
                }
            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

    
asked by anonymous 09.09.2014 / 15:06

2 answers

3

To find a Array within a JSONArray is through the getJSONArray .

Using your method:

private void _getCardapio(String result) {
    //...
    try {
        JSONArray json = new JSONArray(result);

        for (int i = 0; i < json.length(); i++) {
            //...

            // Agora voce possui um determinado array na posicao i
            JSONArray arrayDentroDoArray = json.getJSONArray(i);

            // Itera sobre cada objeto do array interno
            for(int j = 0; j < arrayDentroDoArray.length(); ++j) {
                JSONObject objeto = arrayDentroDoArray.getJSONObject(j);
                // Usar o objeto
            }

            //cardapioEntities.setCardapioPreco(convertCardapio(json.getJSONObject(i)).getCardapioPreco());
            //listaLanches.add(cardapioEntities);
        }
        //...
        final ListView lvLanches = (ListView) findViewById(R.id.lvLanches);
        lvLanches.setAdapter(new CustomListAdapter(this, listaLanches));
        //...
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

Be careful because if the type of position i is not a Array it will throw JSONException . What you could do, if you're not sure, is:

Object o = json.get(i);

if(o instanceof JSONArray) {
    // Trabalhar com o Array
} else if(o instanceof JSONObject) {
    // Trabalhar com o objeto
}
    
09.09.2014 / 16:06
1

I think these links will help you with your question!

link

link

link

More would be something like:

 //string json: contém array com três elementos
    String json_str = "{\"elenco\":[\"Json Leigh\", \"Mary Stylesheet\",  \"David Markupovny\"]}";

    //instancia um novo JSONObject passando a string como entrada
    JSONObject my_obj = new JSONObject(json_str);

    //recupera o array "elenco"  
    JSONArray elenco = my_obj.getJSONArray("elenco");
    
09.09.2014 / 16:08