- /**
- * Take the String representing the complete forecast in JSON Format and
- * pull out the data we need to construct the Strings needed for the wireframes.
- * <p/>
- * Fortunately parsing is easy: constructor takes the JSON string and converts it
- * into an Object hierarchy for us.
- */
- private String[] getWeatherDataFromJson(String forecastJsonStr, int numDays)
- throws JSONException {
-
- // These are the names of the JSON objects that need to be extracted.
- final String OWM_LIST = "list";
- final String OWM_WEATHER = "weather";
- final String OWM_TEMPERATURE = "temp";
- final String OWM_MAX = "max";
- final String OWM_MIN = "min";
- final String OWM_DATETIME = "dt";
- final String OWM_DESCRIPTION = "main";
-
- JSONObject forecastJson = new JSONObject(forecastJsonStr);
- JSONArray weatherArray = forecastJson.getJSONArray(OWM_LIST);
-
- String[] resultStrs = new String[numDays];
- for (int i = 0; i < weatherArray.length(); i++) {
- // For now, using the format "Day, description, hi/low"
- String day;
- String description;
- String highAndLow;
-
- // Get the JSON object representing the day
- JSONObject dayForecast = weatherArray.getJSONObject(i);
-
- // The date/time is returned as a long. We need to convert that
- // into something human-readable, since most people won't read "1400356800" as
- // "this saturday".
- long dateTime = dayForecast.getLong(OWM_DATETIME);
- day = getReadableDateString(dateTime);
-
- // description is in a child array called "weather", which is 1 element long.
- JSONObject weatherObject = dayForecast.getJSONArray(OWM_WEATHER).getJSONObject(0);
- description = weatherObject.getString(OWM_DESCRIPTION);
-
- // Temperatures are in a child object called "temp". Try not to name variables
- // "temp" when working with temperature. It confuses everybody.
- JSONObject temperatureObject = dayForecast.getJSONObject(OWM_TEMPERATURE);
- double high = temperatureObject.getDouble(OWM_MAX);
- double low = temperatureObject.getDouble(OWM_MIN);
-
- highAndLow = formatHighLows(high, low);
- resultStrs[i] = day + " - " + description + " - " + highAndLow;
- }