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

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