Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
YassinAJDI committed Aug 1, 2018
1 parent 14492ee commit 8bc36b7
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions app/src/main/java/com/udacity/sandwichclub/utils/JsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,38 @@

public class JsonUtils {

private final static String KEY_NAME = "name";
private final static String KEY_MAIN_NAME = "mainName";
private final static String KEY_ALSO_KNOWN_AS = "alsoKnownAs";
private final static String KEY_PLACE_OF_ORIGIN = "placeOfOrigin";
private final static String KEY_DESCRIPTION = "description";
private final static String KEY_IMAGE_URL = "image";
private final static String KEY_INGREDIENTS = "ingredients";

public static Sandwich parseSandwichJson(String json) throws JSONException {
JSONObject sandwichObject = new JSONObject(json);
// TODO: 30/07/2018 add static names

// name object
JSONObject name = sandwichObject.getJSONObject("name");
JSONObject name = sandwichObject.getJSONObject(KEY_NAME);

// mainName
String mainName = name.getString("mainName");
String mainName = name.getString(KEY_MAIN_NAME);

// Also known as
JSONArray alsoKnownAs = name.getJSONArray("alsoKnownAs");
JSONArray alsoKnownAs = name.getJSONArray(KEY_ALSO_KNOWN_AS);
List<String> knowAs = getStrings(alsoKnownAs);

// Place of origin
String placeOfOrigin = sandwichObject.getString("placeOfOrigin");
String placeOfOrigin = sandwichObject.getString(KEY_PLACE_OF_ORIGIN);

// Description
String description = sandwichObject.getString("description");
String description = sandwichObject.getString(KEY_DESCRIPTION);

// Image
String image = sandwichObject.getString("image");
String image = sandwichObject.getString(KEY_IMAGE_URL);

// Ingredients
JSONArray ingredientsJson = sandwichObject.getJSONArray("ingredients");
JSONArray ingredientsJson = sandwichObject.getJSONArray(KEY_INGREDIENTS);
List<String> ingredients = getStrings(ingredientsJson);

return new Sandwich(mainName, knowAs, placeOfOrigin, description, image, ingredients);
Expand Down

0 comments on commit 8bc36b7

Please sign in to comment.