Obtain if there is data in a nested object inside a json

2

I run into a problem, I have a list of items in a json. and before filling out a list, I want to check if there is a PROMOS sector in the json. Try this way but it only brings me the first 5 articles and nothing from the PROMOS sector

my java class

 protected String doInBackground(String... strings) {
        prod = new ArrayList<>();
        lista_eligida = (ListView) findViewById(R.id.lista);

        String url = "http://danbijann.freeiz.com/tareas.json";
        HttpHandler sh = new HttpHandler();
        JSONObject jsonStr = sh.makeServiceCall(url);


            try {
                JSONObject jsonObj = new JSONObject(String.valueOf(jsonStr));
                Log.e("JSON", String.valueOf(jsonStr));
                JSONArray contacts = jsonObj.getJSONArray("PEDIDOS");
                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(i);

                    String codig = c.getString("codigo");
                    String des = c.getString("descrip");
                    String prec = c.getString("precio");
                    String cantidad = c.getString("cantidad");
                    String ubica="";
                    String prueba = "";
                    String prueba2 = "";

                    String promocion = "";

                    if (jsonObj.has("PROMOS") && !jsonObj.isNull("PROMOS")) {
                        JSONArray promos = jsonObj.getJSONArray("PROMOS");
                        for(int j=0;j<promos.length();j++)
                        {


                            if(promos.get(j).equals("ubica") && !promos.get(j).equals("ubica")) {
                                ubica = c.getString("ubica");
                            }

                            if(promos.get(j).equals("promocion") && !promos.get(j).equals("promocion")) {
                                promocion = c.getString("promocion");
                            }

                            if(promos.get(j).equals("prueba") && !promos.get(j).equals("prueba")){
                                prueba = c.getString("prueba");
                            }

                            if(promos.get(j).equals("prueba2") && !promos.get(j).equals("prueba2")) {
                                prueba2 = c.getString("prueba2");
                            }

                        }
                    }


                    HashMap<String, String> contacto = new HashMap<>();
                    contacto.put("codigo", codig);
                    contacto.put("descrip", des);
                    contacto.put("precio", prec);
                    contacto.put("cantidad", cantidad);
                    contacto.put("ubica", ubica);
                    contacto.put("preuba", prueba);
                    contacto.put("promocion", promocion);
                    contacto.put("prueba2", prueba2);

                    Log.d("ADAP", codig);
                    Log.d("ADAP", des);
                    Log.d("ADAP", prec);
                    Log.d("ADAP", cantidad);
                    Log.d("ADAP", ubica);
                    Log.d("ADAP", prueba);
                    Log.d("ADAP", promocion);
                    Log.d("ADAP", prueba2);


                    prod.add(contacto);
                }
            } catch (final JSONException e) {
                e.getMessage();
            }
        return null;
        }

this is my json file

    { 

   "PEDIDOS":[  

      {  
         "codigo":"AB235695-1",
         "descrip":"MONITOR SAMDUNG DE 32 PULGADAS",
         "precio":"526559",
         "cantidad":"4",
         "ubica":"A:002 S:005 F:001"
      },
      {  
         "codigo":"B005695-5",
         "descrip":"CPU INTEL PRUEBA DE ESPACIO",
         "precio":"526559",
         "cantidad":"2",
         "ubica":"A:002 S:005 F:001",
         "PROMOS":[  
            {  
               "promocion":"50 off",
               "prueba":"datos mas por prueva",
               "prueba2":"otro mas "
            }
         ]
      },

      {  
         "codigo":"AB235-6",
         "descrip":"MONITOR SAMDUNG DE 32 PULGADAS",
         "precio":"526559",
         "cantidad":"2",
         "ubica":"A:002 S:005 F:001"
      },


      {  
         "codigo":"A56895-11",
         "descrip":"MONITOR SAMDUNG DE 32 PULGADAS",
         "precio":"526559",
         "cantidad":"14",
         "ubica":"A:002 S:005 F:001"
      },


      {  
         "codigo":"B04565-50",
         "descrip":"CPU INTEL PRUEBA DE ESPACIO",
         "precio":"526559",
         "cantidad":"20",
         "ubica":"A:002 S:005 F:001",

         "PROMOS":[  
            {  
               "promocion":"50 off",
               "prueba":"datos mas por prueva",
               "prueba2":"otro mas "
            }
         ]
      }
   ]
}
    
asked by user62207 19.10.2018 в 15:55
source

1 answer

4

The right thing to do is to use the has ( )

if (json.has("PROMOS")) {
   ...
   ...
}
  

has () : Returns true if this object has a   assignment for the name.

Actually the problem you present is because when reading the object in the array "PEDIDOS" and find the array "PROMOS" , you must get it from JSONObject child, it must be c and not jsonObj , in this way you will be able to obtain the data without problem within each object:

 JSONObject c = contacts.getJSONObject(i);
 ...
 ...
    if (c.has("PROMOS")/* && !jsonObj.isNull("PROMOS")*/) {
                    JSONArray promos = c.getJSONArray("PROMOS"); /*jsonObj.getJSONArray("PROMOS");*/
                    for(int j=0;j<promos.length();j++)
                    {
                    ...
                    ...

now also when obtaining the data within the "PROMOS" array, you must enter its object to obtain its values:

  if (c.has("PROMOS")) {
                    JSONArray promos = c.getJSONArray("PROMOS"); 
                    for(int j=0;j<promos.length();j++)
                    {


                        if(promos.getJSONObject(j).has("ubica") ) {
                            ubica = promos.getJSONObject(j).getString("ubica");
                        }

                        if(promos.getJSONObject(j).has("promocion")) {
                            promocion = promos.getJSONObject(j).getString("promocion");
                        }

                        if(promos.getJSONObject(j).has("prueba")){
                            prueba = promos.getJSONObject(j).getString("prueba");
                        }

                        if(promos.getJSONObject(j).has("prueba2")) {
                            prueba2 = promos.getJSONObject(j).getString("prueba2");
                        }

                    }
                }

Check the structure of your JSON file:

 { //* OBJETO PADRE 
  "PEDIDOS":[ //* ARRAY PEDIDOS
    {  //* OBJETO DENTRO DE ARRAY PEDIDOS
         "codigo":".....",
         "descrip":".....",
         "precio":".....",
         "cantidad":".....",
         "ubica":".....",
         "PROMOS":[  // * ARRAY DENTO DE OBJETO PEDIDOS
            {    // * OBJETO DENTRO DE ARRAY PROMOS
               "promocion":".....",
               "prueba":".....",
               "prueba2":"....."
            }
         ]
      },
   ...
   ]
 }

This would be the code with the corrections discussed above:

   try {
        JSONObject jsonObj = new JSONObject(String.valueOf(jsonStr));
        Log.e("JSON", String.valueOf(jsonStr));
        JSONArray contacts = jsonObj.getJSONArray("PEDIDOS");
        for (int i = 0; i < contacts.length(); i++) {
            JSONObject c = contacts.getJSONObject(i);

            String codig = c.getString("codigo");
            String des = c.getString("descrip");
            String prec = c.getString("precio");
            String cantidad = c.getString("cantidad");
            String ubica="";
            String prueba = "";
            String prueba2 = "";

            String promocion = "";

            if (c.has("PROMOS")) {
                JSONArray promos = c.getJSONArray("PROMOS"); 
                for(int j=0;j<promos.length();j++)
                {

                    if(promos.getJSONObject(j).has("ubica") ) {
                        ubica = promos.getJSONObject(j).getString("ubica");
                    }

                    if(promos.getJSONObject(j).has("promocion")) {
                        promocion = promos.getJSONObject(j).getString("promocion");
                    }

                    if(promos.getJSONObject(j).has("prueba")){
                        prueba = promos.getJSONObject(j).getString("prueba");
                    }

                    if(promos.getJSONObject(j).has("prueba2")) {
                        prueba2 = promos.getJSONObject(j).getString("prueba2");
                    }

                }
            }


            HashMap<String, String> contacto = new HashMap<>();
            contacto.put("codigo", codig);
            contacto.put("descrip", des);
            contacto.put("precio", prec);
            contacto.put("cantidad", cantidad);
            contacto.put("ubica", ubica);
            contacto.put("preuba", prueba);
            contacto.put("promocion", promocion);
            contacto.put("prueba2", prueba2);

            Log.d("ADAP", codig);
            Log.d("ADAP", des);
            Log.d("ADAP", prec);
            Log.d("ADAP", cantidad);
            Log.d("ADAP", ubica);
            Log.d("ADAP", prueba);
            Log.d("ADAP", promocion);
            Log.d("ADAP", prueba2);


            prod.add(contacto);
        }
    } catch (final JSONException e) {
        e.getMessage();
    }
    
answered by 19.10.2018 / 16:35
source