Created
September 30, 2016 00:28
-
-
Save cami7ord/067beef106b2e58f6cf0b4296f273223 to your computer and use it in GitHub Desktop.
Recursive categories parsing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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