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

Jake Nguyen
Jake Nguyen
48 Points

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