-1

我的 json 是这样的,我想解析 newsitems 以获取标题、网址、内容、费用标签、网址和 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解析代码是这样的

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

我遇到这样的错误我想解析newsitems以获取标题,url,内容,feelabel,url和feedlabel我使用了本教程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
4

2 回答 2

1

appnews是 JSONObject 所以

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

有一个错字,显然,已经有一个news对象,所以

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);
}
于 2018-01-14T11:44:34.023 回答
0

尝试这个

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();         
}
于 2018-01-14T13:01:20.240 回答