Created
February 4, 2015 22:57
-
-
Save Mikelantonio/538d2c85644ff50ddd01 to your computer and use it in GitHub Desktop.
weather parsing json
This file contains 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 | |
JSONObject object = new JSONObject(weatherJsonStr); | |
JSONArray list = object.getJSONArray("list"); | |
JSONObject forecast = list.getJSONObject(dayIndex).getJSONObject("temp"); | |
return forecast.getDouble("max"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment