Last active
September 25, 2018 08:20
-
-
Save codeamt/6367c9d68a72f69dc96389704f6c8d8f to your computer and use it in GitHub Desktop.
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
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 */ | |
// Handle anomolies first | |
JSONObject name = base.getJSONObject("name"); | |
String mainName = name.getString("mainName"); | |
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"); | |
} catch (JSONException e) { | |
/* Print Any Errors */ | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment