0

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

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

{
"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());
            }
    }

然而,正如您所看到的,“条目”在文本文件中明显位于数组标记“[”之前。我真的很难过。将不胜感激一点帮助。

4

1 回答 1

0

你必须首先得到:

  responseData -> feed -> entries

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

并且只有在:

newsItems = feed.getJSONArray("entries");
于 2013-09-16T13:50:12.343 回答