Error parsing json in android: t user of type org.json.JSONObject can not be converted to JSONArray

1

I have a String that I need to parse and it gives me the following error:

  

Value {"name": "test", "code": "1", "photo": "test"} at user of   type org.json.JSONObject can not be converted to JSONArray

my json's format is as follows:

{"Error":false,"usuario":{"nombre":"prueba","codigo":"1","foto":"prueba"}}

and this is the code I have:

public void parseJson(String respuesta){
        String nombre="d";
        int codigo=0;
        String foto="d";
        try {
            JSONObject jObject = new JSONObject(respuesta);
            Boolean error = jObject.getBoolean("Error");
            if (error){
                String error_msg= jObject.getString("Error_msg");
                Toast.makeText(getApplicationContext(),"Error: "+error_msg,Toast.LENGTH_LONG).show();
            }else {

                JSONArray jArray = jObject.getJSONArray("usuario");

                JSONObject oneObject = jArray.getJSONObject(0);
                nombre = oneObject.getString("nombre");
                foto = oneObject.getString("foto");
                codigo = oneObject.getInt("codigo");

                String mensaje = "tu nombre es: " + nombre + ", tu codigo es: " + codigo + " y tu perfil es: " + foto;
                Toast.makeText(getApplicationContext(), mensaje, Toast.LENGTH_LONG).show();
            }

        } catch (JSONException e) {
            Toast.makeText(getApplicationContext(), "Error al parsear datos", Toast.LENGTH_SHORT).show();
            Log.d("error parseando",e.getMessage());
        }

    }

I hope you can help me

    
asked by zhet 12.12.2017 в 23:49
source

2 answers

0

Check the structure of your json:

{"Error":false,"usuario":{"nombre":"prueba","codigo":"1","foto":"prueba"}}

actually "user" is an object (remember Object: "{", array "["), and not an array, therefore this line will have the problem:

 JSONArray jArray = jObject.getJSONArray("usuario");

you must change to:

 JSONObject jObject2 = jObject.getJSONObject("usuario");

Code:

   public void parseJson(String respuesta){
        String nombre="d";
        int codigo=0;
        String foto="d";
        try {
            JSONObject jObject = new JSONObject(respuesta);
            Boolean error = jObject.getBoolean("Error");
            if (error){
                String error_msg= jObject.getString("Error_msg");
                Toast.makeText(getApplicationContext(),"Error: "+error_msg,Toast.LENGTH_LONG).show();
            }else {

                //*No es Array es Objeto.
                //JSONArray jArray = jObject.getJSONArray("usuario");
                JSONObject jObject2 = jObject.getJSONObject("usuario");

                //*Esta linea no necesaria puesto que ya tienes el objeto.
                //JSONObject oneObject = jArray.getJSONObject(0);
                nombre = jObject2.getString("nombre");
                foto = jObject2.getString("foto");
                codigo = jObject2.getInt("codigo");

                String mensaje = "tu nombre es: " + nombre + ", tu codigo es: " + codigo + " y tu perfil es: " + foto;
                Toast.makeText(getApplicationContext(), mensaje, Toast.LENGTH_LONG).show();
            }

        } catch (JSONException e) {
            Toast.makeText(getApplicationContext(), "Error al parsear datos", Toast.LENGTH_SHORT).show();
            Log.d("error parseando",e.getMessage());
        }

    }
    
answered by 13.12.2017 / 00:05
source
1

The problem is that usuario is a object json , not a array . If you notice the format of usuario is the following:

"usuario":{"nombre":"prueba","codigo":"1","foto":"prueba"}

The format of a array json is as follows:

"frutas":["guineo", "manzana", "pera"]

If you look at the values of an array they are enclosed in [] keys and separated by commas, with which you can easily differentiate which is an object and which is a json array.

The correct way to obtain the values of the object usuario according to your json, is the following:

...

JSONObject usuario = jObject.getJSONObject("usuario");
nombre = usuario.getString("nombre");
foto = usuario.getString("foto");
codigo = usuario.getInt("codigo");

...
    
answered by 13.12.2017 в 00:14