简体   繁体   中英

How parse JSON data into ListView

I would like to visualize the Json data on a listview, but I don't know how to do it ... I tried to use a TextView to verify the correct passage of the data and it seems to work, but I would need to display them on the listView ... ideas?

{"Esito":true,"Dati":[{"id":"357","id_utente":"16","nome_prodotto":"cozze"},{"id":"358","id_utente":"16","nome_prodotto":"riso"},{"id":"362","id_utente":"16","nome_prodotto":"patate"},{"id":"366","id_utente":"16","nome_prodotto":"cozze"},{"id":"367","id_utente":"16","nome_prodotto":null}]}
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.G[enter image description here][1]ET, url, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                    try {

                        JSONArray jsonArray = response.getJSONArray("Dati");

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

                            JSONObject dato = jsonArray.getJSONObject(i);

                            String id = dato.getString("id");
                            String id_utente = dato.getString("id_utente");
                            String nome_prodotto = dato.getString("nome_prodotto");

                            mTextViewResult.append(id + ", " + id_utente +  ", " + nome_prodotto +  "\n\n");
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

Just make new object class and collect data to list :

class YourObiekt {
private String id;
private String idUtente;
private String nomeProdotto;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getIdUtente() {
    return idUtente;
}

public void setIdUtente(String idUtente) {
    this.idUtente = idUtente;
}

public String getNomeProdotto() {
    return nomeProdotto;
}

public void setNomeProdotto(String nomeProdotto) {
    this.nomeProdotto = nomeProdotto;
}

}

 List<YourObiekt> yourObiektList = new ArrayList<YourObiekt>();
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {

                try {

                    JSONArray jsonArray = response.getJSONArray("Dati");


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

                        JSONObject dato = jsonArray.getJSONObject(i);
                        YourObiekt yo = new YourObiekt();

                        yo.setId(dato.getString("id"));
                        yo.setIdUtente(dato.getString("id_utente"));
                        yo.setNomeProdotto(dato.getString("nome_prodotto"));

                        yourObiektList.add(yo);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

And now you get yourObiektList as data for your listView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM