Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid java trouble parsing JSON
    primarykey
    data
    text
    <p>I've parsed some simple json in the past but this one is giving me a bit more trouble. I'm trying to get data from Wikipedia's "on this day in history"</p> <p><a href="http://en.wikipedia.org/w/api.php?format=json&amp;action=query&amp;titles=September_17&amp;rvprop=content&amp;prop=revisions" rel="nofollow">Example URL with JSON output</a></p> <p><a href="http://en.wikipedia.org/wiki/September_17" rel="nofollow">Actual URL in regular HTML</a></p> <p>Now if you pop the JSON URL into: <a href="http://jsonlint.com/" rel="nofollow">http://jsonlint.com/</a> to get a breakdown the first bit of trouble I have is after the object after pages in this case "27993" will always be different so how am I to fetch this if I don't know the name?</p> <p>For testing purposes I have this code so far:</p> <pre><code>JSONObject json = new JSONObject(result); input.close(); JSONObject json2 = new JSONObject(json.getString("query").toString()); JSONObject json3 = new JSONObject(json2.getString("pages").toString()); JSONObject json4 = new JSONObject(json3.getString("27993").toString()); JSONArray contacts = json4.getJSONArray("revisions"); StringBuilder builder = new StringBuilder(); builder.append(contacts.getString(0)); return builder.toString(); </code></pre> <p>That returns everything in the revisions array which leads me to my next question...is there an easy method to parse that output?</p> <p><strong>EDIT: Revised code that solves issue #1 Now about parsing that output?</strong></p> <pre><code> JSONObject json = new JSONObject(result); input.close(); JSONObject json2 = new JSONObject(json.getString("query").toString()); JSONObject json3 = new JSONObject(json2.getString("pages").toString()); Iterator i = json3.keys(); JSONObject j = null; while (i.hasNext()) { try { String key = i.next().toString(); j = json3.getJSONObject(key); } catch (JSONException e) { e.printStackTrace(); } } JSONArray contacts = j.getJSONArray("revisions"); StringBuilder builder = new StringBuilder(); builder.append(contacts.getString(0)); return builder.toString(); </code></pre> <p><strong>The 2nd part of the question is there a simple solution to parsing such output. Here is a snip:</strong></p> <blockquote> <p>":"{{pp-move-indef}}{{September calendar|float=right}}\n{{ThisDateInRecentYears}}\n{{Day}}\n\n==Events==\n* [[456]] &ndash; [[Remistus]], Roman general (''[[magister militum]]''), is [[Siege|besiege]]d with a [[Goths|Gothic]] force at [[Ravenna]] and later executed in the Palace ''in Classis'', outside the city. \n*[[1111]] &ndash; Highest Galician nobility led by [[Pedro Fr\u00f3ilaz de Traba]] and the bishop [[Diego Gelm\u00edrez]] crown [[Alfonso VII of Le\u00f3n and Castile|Alfonso VII]] as \"[[kingdom of Galicia|King of Galicia]]\".\n*[[1176]] &ndash; The [[Battle of Myriokephalon]] is fought.\n*[[1462]] &ndash; The [[Battle of \u015awiecino]] (also known as the Battle of \u017barnowiec) is fought during [[Thirteen Years' War (1454\u201366)|Thirteen Years' War]].\n*[[1577]] &ndash; The [[Peace of Bergerac]] is signed between [[Henry III of France]] and the [[Huguenots]].\n*[[1630]] &ndash; The city of [[Boston]]</p> </blockquote>
    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.
 

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