Skip to content

Instantly share code, notes, and snippets.

@codeamt
Last active December 13, 2018 01:24

Revisions

  1. codeamt revised this gist Sep 25, 2018. 1 changed file with 23 additions and 48 deletions.
    71 changes: 23 additions & 48 deletions JsonUtils-step5.java
    Original file line number Diff line number Diff line change
    @@ -17,55 +17,30 @@ public static Sandwich parseSandwichJson(String json) {
    /* Step One: Convert json string to JSON object */
    JSONObject base = new JSONObject(json);

    /* Step Two: Parse the Base Object's Keys Into Autonomous Objects/Strings for Serializing */
    // Handle anomolies first

    // Parsing the name key
    // Coerce name key to JSON object called "name"
    JSONObject name = base.getJSONObject("name");

    // Access the mainName key of the name object store it's value as String
    String mainName = name.getString("mainName");

    // Coerce the alsoKnownAs key to a JSON array called "altNames"
    JSONArray altNames = name.getJSONArray("alsoKnownAs");

    // Instantiate Array Literal for Alt Names called "altNamesList"
    ArrayList<String> altNamesList = new ArrayList<>();

    // Loop through altNames JSON array
    for(int i=0; i<altNames.length(); i++) {
    // Add each item in altNames to altNamesList
    altNamesList.add(altNames.getString(i));
    }


    // Parsing the ingredients key
    // Coerce ingredients key to a JSON array called "ingredients"
    JSONArray ingredients = base.getJSONArray("ingredients");

    // Instantiate Array Literal for Ingredients called "ingredientsList"
    ArrayList<String> ingredientsList = new ArrayList<>();

    // Loop through ingredients JSON array
    for(int i=0; i<ingredients.length(); i++) {
    // Add each item in ingredients to the ingredientsList
    ingredientsList.add(ingredients.getString(i));
    }


    // Parsing keys with primitive assignments
    // Access the placeOfOrigin key of the base object, store the value as String
    String placeOfOrigin = base.getString("placeOfOrigin");

    // Access the image key of the base object, store the value as String
    String image = base.getString("image");

    // Access the description key from the base object, store the value as String
    String description = base.getString("description");
    /* Step Two: Parse the Base Object's Keys */
    // Handle anomolies first
    JSONObject name = base.getJSONObject("name");
    String mainName = name.getString("mainName");

    // Finally, return a new Sandwich Object
    return new Sandwich(mainName, altNamesList, placeOfOrigin, description, image, ingredientsList);
    JSONArray altNames = name.getJSONArray("alsoKnownAs");
    ArrayList<String> altNamesList = new ArrayList<>();
    for(int i=0; i<altNames.length(); i++) {
    altNamesList.add(altNames.getString(i));
    }

    JSONArray ingredients = base.getJSONArray("ingredients");
    ArrayList<String> ingredientsList = new ArrayList<>();
    for(int i=0; i<ingredients.length(); i++) {
    ingredientsList.add(ingredients.getString(i));
    }

    // Parsing keys with primitive assignments
    String placeOfOrigin = base.getString("placeOfOrigin");
    String image = base.getString("image");
    String description = base.getString("description");

    // Step Three: Finally, return a new Sandwich Object
    return new Sandwich(mainName, altNamesList, placeOfOrigin, description, image, ingredientsList);


    } catch (JSONException e) {
  2. codeamt revised this gist Sep 24, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions JsonUtils-step5.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    package com.udacity.sandwichclub.utils;

    import com.udacity.sandwichclub.model.Sandwich;

    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
  3. codeamt revised this gist Sep 24, 2018. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions JsonUtils-step5.java
    Original file line number Diff line number Diff line change
    @@ -61,6 +61,9 @@ public static Sandwich parseSandwichJson(String json) {

    // Access the description key from the base object, store the value as String
    String description = base.getString("description");

    // Finally, return a new Sandwich Object
    return new Sandwich(mainName, altNamesList, placeOfOrigin, description, image, ingredientsList);


    } catch (JSONException e) {
  4. codeamt created this gist Sep 24, 2018.
    73 changes: 73 additions & 0 deletions JsonUtils-step5.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    package com.udacity.sandwichclub.utils;

    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;

    import java.util.ArrayList;


    public class JsonUtils {

    public static Sandwich parseSandwichJson(String json) {
    try {

    /* Step One: Convert json string to JSON object */
    JSONObject base = new JSONObject(json);

    /* Step Two: Parse the Base Object's Keys Into Autonomous Objects/Strings for Serializing */
    // Handle anomolies first

    // Parsing the name key
    // Coerce name key to JSON object called "name"
    JSONObject name = base.getJSONObject("name");

    // Access the mainName key of the name object store it's value as String
    String mainName = name.getString("mainName");

    // Coerce the alsoKnownAs key to a JSON array called "altNames"
    JSONArray altNames = name.getJSONArray("alsoKnownAs");

    // Instantiate Array Literal for Alt Names called "altNamesList"
    ArrayList<String> altNamesList = new ArrayList<>();

    // Loop through altNames JSON array
    for(int i=0; i<altNames.length(); i++) {
    // Add each item in altNames to altNamesList
    altNamesList.add(altNames.getString(i));
    }


    // Parsing the ingredients key
    // Coerce ingredients key to a JSON array called "ingredients"
    JSONArray ingredients = base.getJSONArray("ingredients");

    // Instantiate Array Literal for Ingredients called "ingredientsList"
    ArrayList<String> ingredientsList = new ArrayList<>();

    // Loop through ingredients JSON array
    for(int i=0; i<ingredients.length(); i++) {
    // Add each item in ingredients to the ingredientsList
    ingredientsList.add(ingredients.getString(i));
    }


    // Parsing keys with primitive assignments
    // Access the placeOfOrigin key of the base object, store the value as String
    String placeOfOrigin = base.getString("placeOfOrigin");

    // Access the image key of the base object, store the value as String
    String image = base.getString("image");

    // Access the description key from the base object, store the value as String
    String description = base.getString("description");


    } catch (JSONException e) {

    /* Print Any Errors */
    e.printStackTrace();
    }
    return null;
    }
    }