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

JSON

Please help me

JSON code below

response: { meta: {}, docs: [ {}, {}, {}, {}, {}, {}, {}, {}, {}, { web_url: "http://www.nytimes.com/2015/05/24/travel/italys-treasured-olive-oil-at-the-source.html", snippet: "In Tuscany and Puglia, making olive oil is a lifestyle, one threatened by bad weather and a killer bacteria.", lead_paragraph: "In Tuscany and Puglia, making olive oil is a lifestyle, one threatened by bad weather and a killer bacteria.", abstract: null, print_page: "1", blog: [ ], source: "The New York Times", multimedia: [], headline: { main: "Italy’s Treasured Olive Oil, at the Source", print_headline: "Sunlight in a Bottle " }, keywords: [], pub_date: "2015-05-24T00:00:00Z", document_type: "article", news_desk: "Travel", section_name: "Travel", subsection_name: null, byline: {}, type_of_material: "News", _id: "555f0c3b38f0d86bef1f67de", word_count: "3084" } ] }, status: "OK", copyright: "Copyright (c) 2013 The New York Times Company. All Rights Reserved."

Below is my code private Headline[] getHeadlines(String jsonData) throws JSONException {

    JSONObject news = new JSONObject(jsonData);
    String status = news.getString("status");
    JSONObject response = news.getJSONObject("response");
    JSONArray docsInJSON = response.getJSONArray("docs");

    Doc[] docs = new Doc[docsInJSON.length()];

    for(int i = 0; i <docsInJSON.length(); i++) {
        JSONObject jsonResult = docsInJSON.getJSONObject(i);

        JSONArray headline = jsonResult.getJSONArray("headline");
        Headline[] headlines = new Headline[headline.length()];
        for (int k = 0;i<headline.length(); k++) {

            JSONObject jsonResultforHeadline = headline.getJSONObject(k);
            Headline headlineFromDoc = new Headline();
            headlineFromDoc.setMain(jsonResultforHeadline.getString("main"));
            headlineFromDoc.setKEY_STATUS(status);

            headlines[i] = headlineFromDoc;
        }
        return headlines;
    }
    return null;
}

and got error this

org.json.JSONException: Value {"content_kicker":"The Pour","main":"Rosé Wine From Provence Deserves to Live a Little"} at headline of type org.json.JSONObject cannot be converted to JSONArray

Please help me

1 Answer