Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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;
}

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