Skip to content

Instantly share code, notes, and snippets.

@victoraguilarc
Created February 17, 2015 21:17
Show Gist options
  • Save victoraguilarc/f052e75e6a6d30402ef1 to your computer and use it in GitHub Desktop.
Save victoraguilarc/f052e75e6a6d30402ef1 to your computer and use it in GitHub Desktop.
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 weather = new JSONObject(weatherJsonStr);
JSONArray list = weather.getJSONArray("list");
JSONObject day = list.getJSONObject(dayIndex);
JSONObject dayTemp = day.getJSONObject("temp");
double max = dayTemp.getDouble("max");
return max;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment