JsonObject Android

2

I am making a listview through a json that I bring from a server. The detail is that I can not pass my data to listview in this function I try to save them from there to move them to listview

public ArrayList<String> ObtDatosJSON(String response) {
    ArrayList<String> listado = new ArrayList<String>();
    try {
        JSONArray jsonArray = new JSONArray(response);

        String texto;
        for (int i=0;i<jsonArray.length();i++){
            texto= jsonArray.getJSONObject(i).getString("Nombre")+ " " +
                       jsonArray.getJSONObject(i).getString("Direccion") + " " +
                       jsonArray.getJSONObject(i).getString("Telefono") + " " ;
            listado.add(texto);
            Log.d("Arreglo de texto",texto);
        }

    }catch(Exception e){
        e.printStackTrace();
    }

This is an example of my JSON:

{
    "Datos": [
        {
            "Nombre": "Fitzgerald, Travis, Prescott, Declan",
            "Direccion": "6088 Tincidunt, Ave",
            "Telefono": "0500 772161"
        },
        {
            "Nombre": "Emmanuel, Buckminster, Karly, Dexter",
            "Direccion": "1711 Luctus Rd.",
            "Telefono": "(016977) 4322"
        },
        {
            "Nombre": "Cecilia, Wynter, Madonna, Kasimir",
            "Direccion": "756-1905 Semper St.",
            "Telefono": "0800 1111"
        },
        {
            "Nombre": "Kelly, Quail, Nasim, Alea",
            "Direccion": "1449 Risus, Av.",
            "Telefono": "0800 518475"
        },
    ]
}

If I remove it "Datos" : my function if it brings them and saves them but if I do not remove it does not get it (enter the JsonException )

Any suggestions?

    
asked by Monster 11.08.2017 в 08:54
source

1 answer

2

The problem is that you are trying to pick up an object as an array, you should collect the object as JSONObject and then take out the array.

JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("Datos");
    
answered by 11.08.2017 / 09:05
source