How to populate sub item spinner with JSON?

150 views Asked by At

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!

2

There are 2 answers

0
Avinash On

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
0
yousef On

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)