1

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);
}
Lucho
  • 51
  • 1
  • 6

2 Answers2

1

The error is because response.getJSONObject(0) is returning a JSONArray and not a JSONObject.

I am guessing you want to fetch the data from that Array. In your On response Method you should add response = response.getJSONArray(0).

    public void onResponse(JSONArray response) {
      try {
          response = response.getJSONArray(0);
          for (int i = 0; i < response.length(); i++) {        

That should fix your problem.

Aditya Kurkure
  • 422
  • 5
  • 19
0
  val stringRequest = object : StringRequest(
            Request.Method.POST, Config."your URL",
            Response.Listener<String> { response ->
                try {

                    val obj = JSONObject(response)

                    if (!obj.getBoolean("error")) {

                        val userssListArray = obj.getJSONArray("arrayname")

                        for (i in 0 until userssListArray.length()) {
                            var usersObj = userssListArray.getJSONObject(i)

                        }


                    } else {

                        if (obj.getString("message") == "Your login expired please re login") {
                            val intent = Intent(this, LoginActivity::class.java)
                            startActivity(intent)
                            finish()
                        }
                    }
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
            },
            Response.ErrorListener {
                Toast.makeText(this, "Network Error Try Again...", Toast.LENGTH_LONG).show()

            }) {
        @Throws(AuthFailureError::class)
        override fun getParams(): Map<String, String> {
            val params = HashMap<String, String>()

            return params
        }
    }
    stringRequest.retryPolicy = DefaultRetryPolicy(
            15000,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)
    //adding request to queue
    VolleySingleton.instance?.addToRequestQueue(stringRequest)

you can use volley like this

developer
  • 135
  • 1
  • 2
  • 11