Skip to content

Instantly share code, notes, and snippets.

@Mikelantonio
Created February 4, 2015 22:57
Show Gist options
  • Save Mikelantonio/538d2c85644ff50ddd01 to your computer and use it in GitHub Desktop.
Save Mikelantonio/538d2c85644ff50ddd01 to your computer and use it in GitHub Desktop.
weather parsing json
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