Json parsin gorg.json.JSONObject无法转换为JSONArray

时间:2018-01-14 11:41:37

标签: android json jsonparser

我的json是这样的,我想解析newsitems来获取tittle,url,内容,费用标签,网址和feedlabel

<nav>
	<image src="assets/apps-btn.png">
	</image>
		<image src="assets/navbar-logo.png">
		</image>
		</nav>

Json解析代码就像这样

{
"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
}
}

我得到这样的错误我想要解析newsitems来获取tittle,url,内容,费用标签,网址和feedlabel我使用了这个教程https://www.tutorialspoint.com/android/android_json_parser.htm

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

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分: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();         
}