Created
December 9, 2014 14:20
-
-
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
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
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