Created
September 16, 2015 03:22
-
-
Save tarla/061031c49f855494f08c to your computer and use it in GitHub Desktop.
Udacity Android JSON Parser
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
// Udacity's code | |
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 { | |
final JSONObject json = new JSONObject(weatherJsonStr); | |
final JSONArray list = json.getJSONArray("list"); | |
final JSONObject dayInfo = list.getJSONObject(dayIndex); | |
final JSONObject tempInfo = dayInfo.getJSONObject("temp"); | |
return tempInfo.getDouble("max"); | |
} | |
/** | |
* Scala syntax looks almost like this: | |
* val max = weatherJsonStr \ "list" \ dayIndex \ "temp" \"max" | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment