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 JSONArray from Openweathermap.org into CurrentWeather?

Values for "weather" from Openweathermap API cannot be converted into JSONObject.

Stephen Sysak
Stephen Sysak
2,025 Points

You're going to need to parse the array from the response by using:

    JSONArray currentWeatherInfo = forecast.getJSONArray("weather");
    JSONObject jsonWeather = currentWeatherInfo.getJSONObject(0);

To populate the icon and description for example you would need to create a String for those then pass that:

    String icon = jsonWeather.getString("icon");
    String summary = jsonWeather.getString("description");

Passed like this:

    currentWeather.setIcon(icon);
    currentWeather.setSummary(summary);

You can easily get the info you need from an online JSON parser such as:

https://jsonformatter.org/json-parser