Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – Combining nested json for loop

Posted on November 14, 2022

Solution 1 :

Instead of parsing the json response manually.
Paste you json here http://www.jsonschema2pojo.org/ and use GSON or Jackson whichever is convenient for parsing the response.
There are many tutorials available which can give you a quick overview of how things can be done. Worth a try if your app has many places where you want to have Json parsing.

Problem :

I have two forloop in which the data is added to different ArrayList. Now my question is how do we combine these two arraylist?
Below is the code which i tried, but not working. Please give me solution for the same. TIA

try {
        JSONObject object = new JSONObject(response);
        status = object.getString("status");
        if (status.equals("200")) {
            rest.dismissProgressdialog();
            JSONArray jsonArray = object.getJSONArray("data");

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                New_Service_Model_1 new_service_model = new New_Service_Model_1();
                new_service_model.setMain_name(jsonObject.getString("main_name"));
                new_service_model.setSmid(jsonObject.optInt("smid"));

                JSONArray jsonArray1 = jsonObject.getJSONArray("sub_service");

                //sub_services_list.clear();
                sub_services_list = new ArrayList<>();
                for (int j = 0; j < jsonArray1.length(); j++) {
                    JSONObject jsonObject1 = jsonArray1.getJSONObject(j);
                    New_Sub_Service_Model_1 sub_service_model = new New_Sub_Service_Model_1();
                    sub_service_model.setSmid(jsonObject1.optString("smid"));
                    sub_service_model.setSbid(jsonObject1.optInt("sbid"));
                    sub_service_model.setSub_name(jsonObject1.optString("sub_name"));
                    sub_service_model.setDesc(jsonObject1.optInt("desc"));
                    sub_service_model.setSt_cust(jsonObject1.optInt("st_cust"));
                    sub_service_model.setSt_pro(jsonObject1.optInt("st_pro"));
                    sub_service_model.setExist_cust(jsonObject1.optInt("exist_cust"));
                    sub_service_model.setExist_prov(jsonObject1.optInt("exist_prov"));
                    sub_services_list.add(sub_service_model);

                }
                new_service_model.setSub_service_list(sub_services_list);
                services_list.add(new_service_model);
                service_list_adapter = new Service_List_Adapter_1(this, services_list, service_costMain_interface);
                rclyrview_services.setAdapter(service_list_adapter);
                service_list_adapter.notifyDataSetChanged();
            }
        } else {
            rest.dismissProgressdialog();
            Toast.makeText(Service_Manage.this, "No Data found", Toast.LENGTH_SHORT).show();
        }

Comments

Comment posted by Navjot

What exactly you mean by combine these two arraylist? As I can see

Comment posted by Meenakshi Ruby

i need to get all data together into a single ArrayList services_list

Comment posted by Meenakshi Ruby

when i run the above code the services_list is always null

Comment posted by Navjot

Can you please share your raw JSON response?

READ  [ANSWERED] java - retrofit E/API: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
Powered by Inline Related Posts

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress