Skip to content Skip to sidebar Skip to footer

Android Parse Jsonobjects Inside Jsonobject

How can I parse this JSON file correctly in Android? I need all the objects from lets say tower 1 (there can be different amounts of weekdays in there) and also different amount of

Solution 1:

if your json string keys are dynamic then you can parse it as:

JSONObject root = new JSONObject(yourString);
// get towers JSONObject
JSONObject towers = root.getJSONObject("towers"); 
// get all Towers name from towers JSONObject 

JSONArray alltowerslist=towers.names();

for(int i=0;i<alltowerslist.length();i++){
   // get sub towers from towers
   JSONObject sub_towers = towers.getJSONObject(alltowerslist.optString(i)); 
   // get days list from sub_towers
    JSONArray alldayslist=sub_towers.names();
    for(int j=0;j<alldayslist.length();j++){
       // get days from  sub_towers
        JSONObject days_json = sub_towers.getJSONObject(alldayslist.optString(j)); 

         // get time json JSONObject from days_json
        JSONArray alltimeslist=days_json.names();
        for(int k=0;k<days_json.length();k++){
       // get time from  sub_towers
        JSONObject time_json = days_json.getJSONObject(alltimeslist.optString(k));

        // now get all value1 from time_json
        JSONArray allvalelist=time_json.names();
        for(int l=0;l<time_json.length();l++){
           String str_value = time_json.optString(allvalelist.optString(l));
          }   
        }
    }
}

Solution 2:


Post a Comment for "Android Parse Jsonobjects Inside Jsonobject"