android谷歌新闻json解析灾难
我正在写一个Android新闻阅读器作为学习练习,在解析数据时遇到了很多困难。我到处都找过了。我发现的教程使用的是android谷歌新闻json解析灾难,android,json,Android,Json,我正在写一个Android新闻阅读器作为学习练习,在解析数据时遇到了很多困难。我到处都找过了。我发现的教程使用的是JSON流,它的结构比谷歌发送的要简单得多 这里是我捕获的字符串片段——JSON数组以“entries”开头,我显示了两个条目,为了简洁起见,删除了第二个条目的中间部分 { "responseData": { "feed": { "feedUrl": "http://news.google.com/news?output\u003drss",
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();
}
但是,正如您所看到的,文本文件中的数组标记“[”前面显然是“entries”。我真的很困惑。希望您能提供一些帮助。您必须首先获得:
responseData -> feed -> entries
JSONObject responseData = Jobj.getJSONObject("responseData");
JSONObject feed = responseData.getJSONObject("feed");
只有在:
newsItems = feed.getJSONArray("entries");
嵌套对象-明白了。谢谢你这么快的回复。我认为它似乎忽略了内容部分,因为所有的格式。在调试器中,它落在那一行,没有错误,但变量窗口中也没有条目。我不认为我需要它,但好奇它是否可以使用。我相信这就是可以从中提取缩略图的地方。此外,Categories部分是提要数组中的一个数组。因此,我将使用:categoryItems=feed.getJSONArray(Categories)来解决它;如果您有Notepadd++,请为JSON视图安装插件请忽略前面的注释-内容很好。看起来像CSS。感谢所有帮助。
newsItems = feed.getJSONArray("entries");