Note that there are some explanatory texts on larger screens.

plurals
  1. POandroid google news json parsing woes
    primarykey
    data
    text
    <p>I am writing an Android news reader as a learning exercise and having lots of trouble parsing the data. I have searched everywhere. The tutorials I have found use <code>JSON</code> streams that are of a much simpler structure than what google is sending.</p> <p>Here is a snippet of the string I capture - the JSON array starts with <em>"entries"</em> and I am showing two entries, with the middle of the second one deleted for brevity.</p> <pre><code>{ "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 } </code></pre> <p>So I have successfully captured the string and now want to create a <code>JSONArray</code> and extract the <code>JSONObjects</code> from it. Here is the code:</p> <pre><code>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 &lt; 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(); } </code></pre> <p>I am catching the <code>JSONException</code> with the message "No values for entries" from the very </p> <p>first line of code: <code>newsItems = Jobj.getJSONArray(ENTRIES);</code> </p> <p>The parameter jobj and call to this method is created <code>onPostExecute</code> like this: </p> <pre><code>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()); } } </code></pre> <p>Yet as you can see, <em>"entries"</em> clearly precedes the array marker <em>"["</em> in the text file. I am truly stumped. Would appreciate a little help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload