Json parsin gorg.json.JSONObject cannot be converted to JSONArray

My json is like this i want to parse newsitems to get tittle,url,contents,feelabel,url and feedlabel

{
"appnews": {
    "appid": 466560,
    "newsitems": [
        {
            "gid": "2284879949551103234",
            "title": "These are the top 100 Steam games of 2017",
            "url": "http://store.steampowered.com/news/externalpost/rps/2284879949551103234",
            "is_external_url": true,
            "author": "contact@rockpapershotgun.com (Alec Meer)",
            "contents": "Another year over, a new one just begun, which means, impossibly, <em>even more games.</em> But what about last year? Which were the games that most people were buying and, more importantly, playing? As is now something of a tradition, Valve have let slip a big ol&#8217; breakdown of the most successful titl...",
            "feedlabel": "Rock, Paper, Shotgun",
            "date": 1514919601,
            "feedname": "rps",
            "feed_type": 0,
            "appid": 550
        },
        {
            "gid": "2284879949546841782",
            "title": "ShiroGames - Bye bye 2017 and WELCOME 2018 !!!",
            "url": "http://store.steampowered.com/news/externalpost/steam_community_announcements/2284879949546841782",
            "is_external_url": true,
            "author": "Lord_brioche",
            "contents": "Hello Northgardians, As the end of 2017 is upon us, we wanted to share with you a few thoughts. What a year! At the beginning of 2017, Northgard was about to start its 6-months Early Access period. At the time, the game was supposed to be a fun, solo strategy game with 4 clans and ShiroGames was onl...",
            "feedlabel": "Community Announcements",
            "date": 1514818282,
            "feedname": "steam_community_announcements",
            "feed_type": 1,
            "appid": 466560
        },
        {
            "gid": "2284879949521363459",
            "title": "Best PC games of 2017",
            "url": "http://store.steampowered.com/news/externalpost/rps/2284879949521363459",
            "is_external_url": true,
            "author": "contact@rockpapershotgun.com (RPS)",
            "contents": "The calendar&#8217;s doors have been opened and the games inside have been eaten. But fear not, latecomer &#8211; we&#8217;ve reconstructed the list in this single post for easy re-consumption. Click on to discover the best games of 2017. <a href=\"https://www.rockpapershotgun.com/2017/12/25/best-pc-games-of-2017/#more-503951\" class=\"more-link\">(more&hellip;)</a> ",
            "feedlabel": "Rock, Paper, Shotgun",
            "date": 1514206814,
            "feedname": "rps",
            "feed_type": 0,
            "appid": 240720
        }
    ]
    ,
    "count": 57
}
}

Json parsing code is like this

 @Override
    protected Void doInBackground(Void... arg0) {
        News news = new News();
        HttpHandler sh = new HttpHandler();
        // Making a request to url and getting response
        String url = "http://api.steampowered.com/ISteamNews/GetNewsForApp/v0002/?appid=466560&count=3&maxlength=300&format=json";
        String jsonStr = sh.makeServiceCall(url);

      ;
        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);


                JSONArray contacts = jsonObj.getJSONArray("appnews");


                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(i);

                    news.setTittle( c.getString("title"));
                    news.setContent(c.getString("content"));
                    news.setDate(c.getString("date"));
                    news.setFeed(c.getString("feedlabel"));
                    news.setUrl(c.getString("url"));

                    newsList.add(news);



                }

            } catch (final JSONException e) {

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                    }
                });

            }

        } else {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),
                            "Couldn't get json from server",
                            Toast.LENGTH_LONG).show();
                }
            });
        }

        return null;
    }

I got error like this i want to parse newsitems to get tittle,url,contents,feelabel,url and feedlabel i used this tutorial https://www.tutorialspoint.com/android/android_json_parser.htm

org.json.JSONException: Value {"appid":466560,"newsitems":[{"gid":"2284879949551103234","title":"These are the top 100 Steam games of 2017","url":"http:\/\/store.steampowered.com\/news\/externalpost\/rps\/2284879949551103234","is_external_url":true,"author":"contact@rockpapershotgun.com (Alec Meer)","contents":"Another year over, a new one just begun, which means, impossibly, <em>even more games.<\/em> But what about last year? Which were the games that most people were buying and, more importantly, playing? As is now something of a tradition, Valve have let slip a big ol&#8217; breakdown of the most successful titl...","feedlabel":"Rock, Paper, Shotgun","date":1514919601,"feedname":"rps","feed_type":0,"appid":550},{"gid":"2284879949546841782","title":"ShiroGames - Bye bye 2017 and WELCOME 2018 !!!","url":"http:\/\/store.steampowered.com\/news\/externalpost\/steam_community_announcements\/2284879949546841782","is_external_url":true,"author":"Lord_brioche","contents":"Hello Northgardians, As the end of 2017 is upon us, we wanted to share with you a few thoughts. What a year! At the beginning of 2017, Northgard was about to start its 6-months Early Access period. At the time, the game was supposed to be a fun, solo strategy game with 4 clans and ShiroGames was onl...","feedlabel":"Community Announcements","date":1514818282,"feedname":"steam_community_announcements","feed_type":1,"appid":466560},{"gid":"2284879949521363459","title":"Best PC games of 2017","url":"http:\/\/store.steampowered.com\/news\/externalpost\/rps\/2284879949521363459","is_external_url":true,"author":"contact@rockpapershotgun.com (RPS)","contents":"The calendar&#8217;s doors have been opened and the games inside have been eaten. But fear not, latecomer &#8211; we&#8217;ve reconstructed the list in this single post for easy re-consumption. Click on to discover the best games of 2017. <a href=\"https:\/\/www.rockpapershotgun.com\/2017\/12\/25\/best-pc-games-of-2017\/#more-503951\" class=\"more-link\">(more&hellip;)<\/a> ","feedlabel":"Rock, Paper, Shotgun","date":1514206814,"feedname":"rps","feed_type":0,"appid":240720}],"count":57} at appnews of type org.json.JSONObject cannot be converted to JSONArray

2 answers

  • answered 2018-01-14 11:41 Pavneet_Singh

    appnews is JSONObject so

    // fetch appnews jsonobject
    JSONArray news= jsonObj.getJSONObject("appnews");
    // fetch the news items jsonarray which actually contains your data
    JSONArray contacts = news.getJSONArray("newsitems");
    

    There is a typo and apparently, there's already a news object so

    JSONArray appNews= jsonObj.getJSONObject("appnews");
    JSONArray contacts = appNews.getJSONArray("newsitems");
    
    for (int i = 0; i < contacts.length(); i++) {
        JSONObject c = contacts.getJSONObject(i);
        news.setTittle( c.getString("title"));
        news.setContent(c.getString("contents"));
        news.setDate(c.getString("date"));
        news.setFeed(c.getString("feedlabel"));
        news.setUrl(c.getString("url"));
    
        newsList.add(news);
    }
    

  • answered 2018-01-14 11:41 Ratilal Chopda

    Try This

    try 
    {
              JSONObject jsonObj = new JSONObject(jsonStr);
    
    
              JSONObject jsonObjAppnews  = jsonObj.getString("appnews");
    
              JSONArray contacts = jsonObjAppnews.getJSONArray("newsitems");
    
              for (int i = 0; i < contacts.length(); i++) {
    
                JSONObject c = contacts.getJSONObject(i);
    
                news.setTittle( c.getString("title"));
                news.setContent(c.getString("contents"));
                news.setDate(c.getString("date"));
                news.setFeed(c.getString("feedlabel"));
                news.setUrl(c.getString("url"));
    
                newsList.add(news);
    
             }
    
    } 
    catch (final JSONException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();         
    }