简体   繁体   English

Android Java解析JSON时遇到麻烦

[英]Android java trouble parsing JSON

I've parsed some simple json in the past but this one is giving me a bit more trouble. 我过去解析过一些简单的json,但这给我带来了更多麻烦。 I'm trying to get data from Wikipedia's "on this day in history" 我正在尝试从Wikipedia的“历史上的这一天”获取数据

Example URL with JSON output 带有JSON输出的示例URL

Actual URL in regular HTML 常规HTML中的实际URL

Now if you pop the JSON URL into: http://jsonlint.com/ 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? 现在,如果将JSON URL弹出到: http : //jsonlint.com/中以获得详细信息,那么我遇到的第一个麻烦就是在这种情况下,“ 27993”页面之后的对象总是不同,所以我该如何获取如果我不知道名字呢?

For testing purposes I have this code so far: 到目前为止,出于测试目的,我有以下代码:

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();

That returns everything in the revisions array which leads me to my next question...is there an easy method to parse that output? 这会返回修订数组中的所有内容,这将导致我下一个问题...是否有一种简单的方法来解析该输出?

EDIT: Revised code that solves issue #1 Now about parsing that output? 编辑:解决问题#1的修订代码现在有关解析该输出?

            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();

The 2nd part of the question is there a simple solution to parsing such output. 问题的第二部分是解析此类输出的简单解决方案。 Here is a snip: 这是一个片段:

":"{{pp-move-indef}}{{September calendar|float=right}}\\n{{ThisDateInRecentYears}}\\n{{Day}}\\n\\n==Events==\\n* [[456]] – [[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. “:” {{pp-move-indef}} {{9月日历| float = right}} \\ n {{ThisDateInRecentYears}} \\ n {{Day}} \\ n \\ n == Events == \\ n * [[ 456]] – [[Remistus]],罗马将军(“ [[magister militum]]''),在[[Ravenna]]受[[Goths | Gothic]]力[[Siege | beiege]] d后来在城外的“ Classis”宫被处死。 \\n*[[1111]] – Highest Galician nobility led by [[Pedro Fr\óilaz de Traba]] and the bishop [[Diego Gelm\írez]] crown [[Alfonso VII of Le\ón and Castile|Alfonso VII]] as \\"[[kingdom of Galicia|King of Galicia]]\\".\\n*[[1176]] – The [[Battle of Myriokephalon]] is fought.\\n*[[1462]] – The [[Battle of \Świecino]] (also known as the Battle of \Żarnowiec) is fought during [[Thirteen Years' War (1454\–66)|Thirteen Years' War]].\\n*[[1577]] – The [[Peace of Bergerac]] is signed between [[Henry III of France]] and the [[Huguenots]].\\n*[[1630]] – The city of [[Boston]] \\ n * [[1111]] –由[[Pedro Fr \\ u00f3ilaz de Traba]]和主教[[Diego Gelm \\ u00edrez]]冠冕的最高加利西亚贵族[[Le \\ u00f3n的阿方索七世和卡斯蒂利亚|阿方索七世] \\ n * [[1176]] –战斗了[[Myriokephalon战斗]。\\ n * [[1462]] –的[[]]表示为\\“ [[加里西亚王国|加里西亚国王]] \\”。 \\ u015awiecino之战](也称为\\ u017barnowiec之战)是在[[十三年战争(1454 \\ u201366)|十三年战争]]期间进行的。\\ n * [[1577]] – [[ [[法国亨利三世]和[[胡格诺派]]之间签署了伯杰拉克和平]。\\ n * [[1630]] – [[波士顿]]

JSONObject pages = new JSONObject(json.getJSONObject("pages"));

// Use this to get IDs
Iterator<String> keys = pages.keys();

// Then iterate
while (keys.hasNext()) {
  JSONObject page = new JSONObject(pages.get(keys.next()));
  // Do something with the page
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM