android google news json解析困境

时间:2013-09-16 13:41:14

标签: android json

我正在编写Android新闻阅读器作为学习练习,并且在解析数据时遇到很多麻烦。我到处搜寻。我发现的教程使用的JSON流比谷歌发送的结构简单得多。

以下是我捕获的字符串片段 - JSON数组以“entries”开头,我显示了两个条目,第二个条目的中间部分为了简洁而删除。

{
"responseData": {
    "feed": {
        "feedUrl": "http://news.google.com/news?output\u003drss",
        "title": "Top Stories - Google News",
        "link": "http://news.google.com/news?    pz\u003d1\u0026amp;ned\u003dus\u0026amp;hl\u003den",
        "author": "",
        "description": "Google News",
        "type": "rss20",
        "entries": [{
            "title": "Major air rescue planned in flooded Colorado county - Fox News",
            "link": "http://news.google.com/news/url?    sa\u003dt\u0026fd\u003dR\u0026usg\u003dAFQjCNHCJS1c-eurSg-     8tAt0PjZ4tiaLdA\u0026url\u003dhttp://www.foxnews.com/weather/2013/09/15/colorado-braces-for-more-heavy-rain-deadly-floods/",
            "author": "",
            "publishedDate": "Mon, 16 Sep 2013 04:49:21 -0700",
            "contentSnippet": "U.S. News \u0026 World ReportMajor air rescue planned in flooded     Colorado countyFox NewsResidents of Boulder County, Colorado are ...",
            "content": "\u003ctable border\u003d\"0\" cellpadding\u003d\"2\" cellspacing\u003d\"7\"        3d\"\"border\u003d\"1\" width\u003d\"80\" height\u003d\"80\"\u003e\u003cbr\u003e\u003cfont   size\u003d\"-2\"\u003eU.S. News \u0026amp;  03e\u003c/font\u003e\u003cbr\u003e\u003cfont  size\u003d\"-1\"\u003eResidents of Boulder County, Colorado are being asked to help guide  helicopter pilots to their locations Monday as a major air rescue is being planned to take  advantage of a clear weather forecast. \u0026quot;The pilots are going to go anywhere and  everywhere they \u003cb\u003e...\u003c/b\u003e\u003c/font\u003e\u003cbr\u003e\u003cfont  size\u003d\"-1\"\u003e\u003ca href\u003d\"http://news.google.com/news/url? sa\u003dt\u0026amp;fd\u003dR\u0026amp;usg\u003dAFQjCNHc6lIe9u_YShLkh7NV5WR9rO6YHQ\u0026amp; url\u003dhttp://www.therepublic.com/view/story/b663f09bbf48403c9041a86623fe428e/CO-- Colorado-Flooding-National-Guard\"\u003eNational Guard members trapped during evacuations  from flooded Colorado town\u003c/a\u003e\u003cfont size\u003d\"-1\"  color\u003d\"#6f6f6f\"\u003eThe  Republic\u003c/font\u003e\u003c/font\u003e\u003cbr\u003e\u003cfont  size\u003d\"-1\"\u003e\u003ca href\u003d\"http://news.google.com/news/url? sa\u003dt\u0026amp;fd\u003dR\u0026amp;usg\u003dAFQjCNFij70BTG-5dO69JM0BVO32S0S- aA\u0026amp;url\u003dhttp://www.cnn.com/2013/09/15/us/colorado-flooding/? hpt%3Dhp_t1\"\u003eColorado floods: More than 500 still unaccounted for as  \u0026#39;devastating\u0026#39; rain looms\u003c/a\u003e\u003cfont size\u003d\"-1\"   Radio\u003c/a\u003e\u003c/font\u003e\u003cbr\u003e\u003cfont size\u003d\"-1\"\u003e\u003ca  href\u003d\"http://news.google.com/news/more? ncl\u003ddQHisuNx5u46LtMZh5z80DBxCCRjM\u0026amp;ned\u003dus\u0026amp;topic\u003dh\"\u003e\u 003cb\u003eall 1,507 news articles  »\u003c/b\u003e\u003c/a\u003e\u003c/font\u003e\u003c/div\u003e\u003c/font\u003e\u003c/td\u0 03e\u003c/tr\u003e\u003c/table\u003e",
            "categories": ["Top Stories"]
        },
        {
            "title": "Costa Concordia salvage begins: Will ship stay in one piece during righting? -    CNN",
            "link": "http://news.google.com/news/url? sa\u003dt\u0026fd\u003dR\u0026usg\u003dAFQjCNFD_8vBF3Gb6B2_6DnbCDwMELEkFQ\u0026url\u003dhtt p://www.cnn.com/2013/09/15/world/europe/italy-costa-concordia-salvage/",
            "author": "",
            ....deletedcontentsforbrevity...."categories": ["Top Stories"]
        }]
    }
},
"responseDetails": null,
"responseStatus": 200
}

所以我已经成功捕获了字符串,现在想要创建一个JSONArray并从中提取JSONObjects。这是代码:

private void parseJSONString( JSONObject Jobj) throws IOException, JSONException {

    try {
        // Getting Array of news
        newsItems = Jobj.getJSONArray(ENTRIES);

        // looping through All Contacts
        for(int i = 0; i < newsItems.length(); i++){
            JSONObject c = newsItems.getJSONObject(i);

            // Storing each json item in variable
            String title = c.getString(TITLE);
            String link = c.getString(LINK);
            String author = c.getString(AUTHOR);
            String pubDate = c.getString(PUBLISHED_DATE);
            String content = c.getString(CONTENT);



        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

我正在抓取JSONException,其中包含来自

的消息“没有条目值”

第一行代码:newsItems = Jobj.getJSONArray(ENTRIES);

参数jobj和此方法的调用创建onPostExecute,如下所示:

protected void onPostExecute(Void result) {
        mTextView.setText(mDataString);
        if (mDataString != null)
            try {

                mJSONObj = new JSONObject(mDataString);
                parseJSONString(mJSONObj);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                Log.e("JSON Parser", "Error converting string to  json " + e.toString());
            }
    }

然而,正如您所看到的,“entries”明显位于文本文件中的数组标记“[”之前。我真的很难过。希望得到一些帮助。

1 个答案:

答案 0 :(得分:0)

你必须先得到:

  responseData -> feed -> entries

JSONObject responseData = Jobj.getJSONObject("responseData");
JSONObject feed  = responseData.getJSONObject("feed");

并且仅在:

之后
newsItems = feed.getJSONArray("entries");