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 Introducing JSONObject

How to parse JSONData from accuweather.com

For those using the AccuWeather API the JSONData is structured differently. The JSONObject is inside an JSONArray. Here is a code snippet showing how to parse the JSONData from accuweather.com in the current lesson.

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

    Log.i(TAG,"From JSON: : " + LocalObservationDateTime);
    return null;
}

1 Answer

So, I decided to just get the EffectiveDate, which is part of the Headline object in the JSON accuweather returns. Took a bit of playing, but I realized you have to get the object "Headline" before you can get the parameter "EffectiveDate". Here's my code. It logged correctly.

private CurrentWeather getCurrentDetails(String jsonData) throws JSONException {
        JSONObject forecast = new JSONObject(jsonData);

        String date = forecast.getJSONObject("Headline").getString("EffectiveDate");
        Log.i(TAG, "From JSON: " + date);
        return null;
    }