I have a problem when try get the json object from json array. The catched error say that JSONArray cannot be convertered to JSONObject. Effectively can get JSON response from web service, but i don't know how convert array to object
error message
org.json.JSONException: Value [{"codaspirante":"1","nombre":"Saul","apellido":"Goodman","mail":"sg@g.com","telefono":"012034948123","codusuario":"0"},{"codaspirante":"2","nombre":"Lalo","apellido":"Salamanca","mail":"ls@g.com","telefono":"12351233","codusuario":"10"},{"codaspirante":"3","nombre":"Walter","apellido":"White","mail":"ww@g.com","telefono":"54843439","codusuario":"10"},{"codaspirante":"4","nombre":"Gustavo","apellido":"Frings","mail":"gf@g.com","telefono":"845738848434","codusuario":"10"}] at 0 of type org.json.JSONArray cannot be converted to JSONObject
Method:
private void buscarCandidatos_Serivicio(String URL){
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, URL, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
for (int i = 0; i < response.length(); i++) {
JSONObject candidato = response.getJSONObject(i);
Candidato c = new Candidato(
candidato.getInt("codaspirante"),
candidato.getString("nombre"),
candidato.getString("apellido"),
candidato.getString("mail"),
candidato.getString("telefono"),
candidato.getInt("codusuario")
);
listaCandidatos.add(c);
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), e.getMessage().toString(), Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
}
);
RequestQueue rq= Volley.newRequestQueue(this);
rq.add(jsonArrayRequest);
}