0

I'm sure this is asked plenty and i've found some questions on here similar but none really got solve my problem. I'm hoping someone can help me out.

What I want to do is present the user with a dropdown (spinner) with a list of city. And in the list of city have a sublist. The list and sublist is from JSON.

User users can only selected sub list.

This my JSON:

{
    "data":{
        "City":[
            {
                "city_id":112,
                "name":"Jakarta",
                "school":[
                    {
                        "school_id":1,
                        "parent":112,
                        "school":"Junior 1"
                    },
                    {
                        "school_id":2,
                        "parent":112,
                        "school":"Junior 2"
                    }
                ]
            },
            {
                "city_id":113,
                "name":"Jakarta",
                "school":[
                    {
                        "school_id":3,
                        "parent":113,
                        "school":"High 1"
                    }
                ]
            }
        ]
    } 
}

this my parsing code :

for (int i=0; i<city.length();i++){
                            JSONObject listcity = city.getJSONObject(i);


                            JSONArray school = listcity.getJSONArray("school");
                            for (int j=0; j<school.length(); j++){
                                JSONObject listschool = school.getJSONObject(j);


                                VolleyLog.e("List Desa =======>"+listschool);
                            }

                        }

expected output :

Jakarta

  • Junior 1
  • junior 2

Tokyo

  • High 1

So far i have been parsing JSON data, I would appreciate if anyone could show the solution of the described problem or provide any link useful to solve the problem.

Thanks!

rangga ario
  • 19
  • 1
  • 8
  • 1
    You have already parse the data ? Can you share your codes? What is the exact probelm. You should describe it. – mrtcnkryln Jan 30 '20 at 05:51
  • Please take a look at [this answer](https://stackoverflow.com/a/18762158/7948109) here 2 separate spinners are being used, my suggestion will be to show a dialog with RecyclerView (city names). clicking on item will show popUp menu with School names, If you explain a bit more we can help you more. – Rahul Gaur Jan 30 '20 at 05:52
  • Could you share what you have tried so far? what you need is to convert that JSON into an object to easily access child information, there a lots of libraries [just to do that](https://stackoverflow.com/a/37572764/3579960). then [use this answer](https://stackoverflow.com/a/18762158/3579960) to achive nested spinner – Coding Otaku Jan 30 '20 at 05:52
  • @RahulGaur I only want to use 1 spinner to populate data from json – rangga ario Jan 30 '20 at 06:08

2 Answers2

0

try with this one you can get city and school name by this.

     try {
        JSONObject objects=new JSONObject(object1);
        JSONObject data=objects.optJSONObject("data");
        JSONArray array=data.optJSONArray("City");
        for(int j=0; j<array.length(); j++)
        {
            String mainSchool=array.optJSONObject(j).optString("name");
            Log.e("mainSchool",mainSchool);
            JSONArray subList=array.optJSONObject(j).optJSONArray("school");

            for(int k=0; k<subList.length(); k++) {
                String subItems=subList.optJSONObject(k).optString("school");
                Log.e("subSchool",subItems);
            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

the output is

 E/mainSchool: Jakarta
 E/subSchool:  Junior 1
               Junior 2
 E/mainSchool: Jakarta
 E/subSchool:  High 1
Avinash
  • 867
  • 4
  • 11
0

you can make clases and use GSON library to parse it jsonObject

// first class is Data
public class data {

private DataBean data;

public DataBean getData() {
    return data;
}

public void setData(DataBean data) {
    this.data = data;
}
}


 /// second class DataBean
public class DataBean {
    private List<CityBean> City;

    public List<CityBean> getCity() {
        return City;
    }

    public void setCity(List<CityBean> City) {
        this.City = City;
    }
  }

 // third class is CityBean 

  public class CityBean {

        private int city_id;
        private String name;
        private List<SchoolBean> school;

        public int getCity_id() {
            return city_id;
        }

        public void setCity_id(int city_id) {
            this.city_id = city_id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<SchoolBean> getSchool() {
            return school;
        }

        public void setSchool(List<SchoolBean> school) {
            this.school = school;
        }
      }

    // forth class is SchoolBean

    public  class SchoolBean {

            private int school_id;
            private int parent;
            private String school;

            public int getSchool_id() {
                return school_id;
            }

            public void setSchool_id(int school_id) {
                this.school_id = school_id;
            }

            public int getParent() {
                return parent;
            }

            public void setParent(int parent) {
                this.parent = parent;
            }

            public String getSchool() {
                return school;
            }

            public void setSchool(String school) {
                this.school = school;
            }
        }

use this library Gson library

and write this line to parse Gson

new Gson().fromJson(/*your response String*/, DataBean.class)
yousef
  • 1,345
  • 1
  • 12
  • 20