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 = newJSONObject(yourString);
// get towers JSONObjectJSONObject 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 towersJSONObject sub_towers = towers.getJSONObject(alltowerslist.optString(i));
// get days list from sub_towersJSONArray alldayslist=sub_towers.names();
for(int j=0;j<alldayslist.length();j++){
// get days from sub_towersJSONObject days_json = sub_towers.getJSONObject(alldayslist.optString(j));
// get time json JSONObject from days_jsonJSONArray alltimeslist=days_json.names();
for(int k=0;k<days_json.length();k++){
// get time from sub_towersJSONObject time_json = days_json.getJSONObject(alltimeslist.optString(k));
// now get all value1 from time_jsonJSONArray allvalelist=time_json.names();
for(int l=0;l<time_json.length();l++){
String str_value = time_json.optString(allvalelist.optString(l));
}
}
}
}
Solution 2:
Just have a look this link of nested json object response parsing....
http://yodi.polatic.me/example-parse-nested-json-array-and-object-java/
Post a Comment for "Android Parse Jsonobjects Inside Jsonobject"