Created
November 29, 2015 16:26
-
-
Save saptak/fbcbf6ef22ae5678c506 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
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
public class WeatherDataParser { | |
/** | |
* Given a string of the form returned by the api call: | |
* http://api.openweathermap.org/data/2.5/forecast/daily?q=94043&mode=json&units=metric&cnt=7 | |
* retrieve the maximum temperature for the day indicated by dayIndex | |
* (Note: 0-indexed, so 0 would refer to the first day). | |
*/ | |
public static double getMaxTemperatureForDay(String weatherJsonStr, int dayIndex) | |
throws JSONException { | |
// TODO: add parsing code here | |
final String OWM_LIST = "list"; | |
final String OWM_TEMPERATURE = "temp"; | |
final String OWM_MAX = "max"; | |
JSONObject forecastJson = new JSONObject(weatherJsonStr); | |
JSONArray weatherArray = forecastJson.getJSONArray(OWM_LIST); | |
// Get the JSON object representing the day | |
JSONObject dayForecast = weatherArray.getJSONObject(dayIndex); | |
// 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); | |
return high; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment