Skip to content

Instantly share code, notes, and snippets.

@josejuansanchez
Created December 9, 2014 14:20
Show Gist options
  • Save josejuansanchez/b1cc196d8c0935e603cc to your computer and use it in GitHub Desktop.
Save josejuansanchez/b1cc196d8c0935e603cc to your computer and use it in GitHub Desktop.
Code Snippet de cómo parsear una respuesta JSON para buscar los atributos que nos interesan
public class WeatherDataParser {
public String getWeatherCityName(String weatherJsonStr) throws JSONException {
JSONObject weatherJson = new JSONObject(weatherJsonStr);
return weatherJson.getString("name");
}
public int getWeatherResponseCode(String weatherJsonStr) throws JSONException {
JSONObject weatherJson = new JSONObject(weatherJsonStr);
return weatherJson.getInt("cod");
}
public String getWeatherDescription(String weatherJsonStr) throws JSONException {
JSONObject weatherJson = new JSONObject(weatherJsonStr);
JSONArray weatherArray = weatherJson.getJSONArray("weather");
return weatherArray.getJSONObject(0).getString("description");
}
public String getWeatherIcon(String weatherJsonStr) throws JSONException {
JSONObject weatherJson = new JSONObject(weatherJsonStr);
JSONArray weatherArray = weatherJson.getJSONArray("weather");
return weatherArray.getJSONObject(0).getString("icon");
}
public String getWeatherTemperature(String weatherJsonStr) throws JSONException {
JSONObject weatherJson = new JSONObject(weatherJsonStr);
return weatherJson.getJSONObject("main").getString("temp");
}
public String getWeatherHumidity(String weatherJsonStr) throws JSONException {
JSONObject weatherJson = new JSONObject(weatherJsonStr);
return weatherJson.getJSONObject("main").getString("humidity");
}
public String getWeatherPressure(String weatherJsonStr) throws JSONException {
JSONObject weatherJson = new JSONObject(weatherJsonStr);
return weatherJson.getJSONObject("main").getString("pressure");
}
public String getWeatherClouds(String weatherJsonStr) throws JSONException {
JSONObject weatherJson = new JSONObject(weatherJsonStr);
return weatherJson.getJSONObject("clouds").getString("all");
}
public String getWeatherWind(String weatherJsonStr) throws JSONException {
JSONObject weatherJson = new JSONObject(weatherJsonStr);
return weatherJson.getJSONObject("wind").getString("speed");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment