Skip to content

Instantly share code, notes, and snippets.

@cami7ord
Created September 30, 2016 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cami7ord/067beef106b2e58f6cf0b4296f273223 to your computer and use it in GitHub Desktop.
Save cami7ord/067beef106b2e58f6cf0b4296f273223 to your computer and use it in GitHub Desktop.
Recursive categories parsing
private void parseResponse(JSONObject response) {
try {
JSONArray categoriesArray = response.getJSONArray("categories");
parseCategoriesArray(categoriesArray);
} catch (JSONException e) {
e.printStackTrace();
}
}
private List<Category> parseCategoriesArray(JSONArray categories) {
List<Category> categoriesList = new ArrayList<>();
try {
if(categories.length() == 0) {
return categoriesList;
} else {
JSONObject categoryObject;
for (int i=0 ; i<categories.length() ; i++) {
categoryObject = categories.getJSONObject(i);
String id = Utilities.getValidJsonString(categoryObject, "_id");
String name = Utilities.getValidJsonString(categoryObject, "name");
String father = Utilities.getValidJsonString(categoryObject, "father");
List<Category> children = parseCategoriesArray(categoryObject.getJSONArray("children"));
categoriesList.add(new Category(id, name, father, children));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return categoriesList;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment