Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Weather App Working with JSON Setting CurrentWeather from JSON

How to parse AccuWeather JSON Data into CurrentWeather Class

For those using the AccuWeather API here is an example showing how to parse the JSON Data into the CurrentWeather class. Don't forget to change the data types of the variables in the CurrentWeather class.

private CurrentWeather getCurrentDetails(String jsonData) throws JSONException {
    JSONArray jsonArray = new JSONArray(jsonData);
    JSONObject row = jsonArray.getJSONObject(0);
    String LocalObservationDateTime = row.getString("LocalObservationDateTime");

    JSONObject temperature = row.getJSONObject("Temperature");
    JSONObject imperial = temperature.getJSONObject("Imperial");
    String value = imperial.getString("Value");

    Log.i(TAG,"From JSON: : " + LocalObservationDateTime);
    Log.i(TAG, "Temperature : " + value);

    CurrentWeather currentWeather = new CurrentWeather();
    currentWeather.setHumidity(row.getInt("RelativeHumidity"));
    currentWeather.setTime(row.getLong("EpochTime"));
    currentWeather.setIcon(row.getInt("WeatherIcon"));
    currentWeather.setLocationLabel("Alcatraz Island, CA");
    currentWeather.setPrecipChance(row.getString("PrecipitationType"));
    currentWeather.setSummary(row.getString("WeatherText"));
    currentWeather.setTemperature(imperial.getDouble("Value"));

    return currentWeather;
}
Alexander Zola
Alexander Zola
9,027 Points

u didnt explain what request you trying to reach, i did 2 reqs for coordinates and then w location key another req, it was really painful and still had issues w parsing JSONObject(but we could parse that string natively), JSONArray doesnt work