-2

我正在使用 wikipedia API,它以 JSON 数组的形式返回结果

接口:

https://en.wikipedia.org/w/api.php?action=query&prop=pageimages&format=json&piprop=thumbnail&pithumbsize=500&pilimit=50&generator=prefixsearch&gpslimit=50&gpssearch=game%20of%20thrones

回复:

{"batchcomplete":"","query":{"pages":{"20715044":{"pageid":20715044,"ns":0,"title":"Game of Thrones","index":1,"thumbnail":{"source":"https://upload.wikimedia.org/wikipedia/commons/thumb/e/e8/Game_of_Thrones_Oslo_exhibition_2014_-_Weapons.jpg/500px-Game_of_Thrones_Oslo_exhibition_2014_-_Weapons.jpg","width":500,"height":395}},"48617894":{"pageid":48617894,"ns":0,"title":"Game of Thrones: Season 1","index":19},"36430376":{"pageid":36430376,"ns":0,"title":"Game of Thrones: Seven Kingdoms","index":12},"32149175":{"pageid":32149175,"ns":0,"title":"Game of Thrones (2012 video game)","index":10},"43181262":{"pageid":43181262,"ns":0,"title":"Game of Thrones (2014 video game)","index":7,"thumbnail":{"source":"https://upload.wikimedia.org/wikipedia/commons/thumb/c/c0/Ty_Franck_2014.jpg/381px-Ty_Franck_2014.jpg","width":381,"height":500}},"33913236":{"pageid":33913236,"ns":0,"title":"Game of Thrones (role-playing video game)","index":18},"31615401":{"pageid":31615401,"ns":0,"title":"Game of Thrones (season 1)","index":3},"34570531":{"pageid":34570531,"ns":0,"title":"Game of Thrones (season 2)","index":9,"thumbnail":{"source":"https://upload.wikimedia.org/wikipedia/commons/thumb/3/3e/Stephen_Dillane_at_Dinard_2012.jpg/375px-Stephen_Dillane_at_Dinard_2012.jpg","width":375,"height":500}},"35436254":{"pageid":35436254,"ns":0,"title":"Game of Thrones (season 3)","index":4},"38710170":{"pageid":38710170,"ns":0,"title":"Game of Thrones (season 4)","index":5},"43186905":{"pageid":43186905,"ns":0,"title":"Game of Thrones (season 5)","index":6,"thumbnail":{"source":"https://upload.wikimedia.org/wikipedia/commons/thumb/7/70/Patio_de_las_doncellas.jpg/500px-Patio_de_las_doncellas.jpg","width":500,"height":330}},"43186937":{"pageid":43186937,"ns":0,"title":"Game of Thrones (season 6)","index":2,"thumbnail":{"source":"https://upload.wikimedia.org/wikipedia/commons/thumb/a/af/Castillo_de_Zafra_-_Exterior.JPG/500px-Castillo_de_Zafra_-_Exterior.JPG","width":500,"height":281}},"35600550":{"pageid":35600550,"ns":0,"title":"Game of Thrones (soundtrack)","index":15},"35911571":{"pageid":35911571,"ns":0,"title":"Game of Thrones Ascent","index":11},"48658545":{"pageid":48658545,"ns":0,"title":"Game of Thrones Awards","index":17},"32770265":{"pageid":32770265,"ns":0,"title":"Game of Thrones CCG","index":16},"38812688":{"pageid":38812688,"ns":0,"title":"Game of Thrones characters","index":13},"39550518":{"pageid":39550518,"ns":0,"title":"Game of Thrones title sequence","index":8},"31535750":{"pageid":31535750,"ns":0,"title":"Game of thrones episodes","index":14}}}}

我想从“pages”数组中获取“title”和“thumbnail”“source”值。

这是我解析响应的代码

JSONObject resultJSON = new JSONObject(result);
JSONObject query = resultJSON.getJSONObject("query");
JSONArray pages = query.getJSONArray("pages");
  for (int i = 0; i < pages.length(); i++) {
        JSONObject j1 = pages.getJSONObject(i);
        Log.e("title", j1.getString("title"));
            if (j1.has("thumbnail")) {
                JSONObject thumbnail = j1.getJSONObject("thumbnail");
                 Log.e("source", thumbnail.getString("source"));
             } else {
                      Log.e("not found", "thumbnail");
                 }
      }

例外

at org.json.JSON.typeMismatch(JSON.java:100)
at org.json.JSONObject.getJSONArray(JSONObject.java:553)
4

3 回答 3

2

同意@AndhanHM 在评论中提到的内容。page是一个JSONObject而不是一个JSONArray。为了迭代那个特定JSONObject的东西,我可能会用mtariq 的这个答案

for (Object key : jsonObj.keySet()) {
        //based on you key types
        String keyStr = (String)key;
        Object keyvalue = jsonObj.get(keyStr);

        //Print key and value
        System.out.println("key: "+ keyStr + " value: " + keyvalue);

        //for nested objects iteration if required
        if (keyvalue instanceof JSONObject)
            printJsonObject((JSONObject)keyvalue);
    }

干杯!

于 2016-06-02T07:41:14.033 回答
1

在您的代码中,“页面”是 JSONObject 而不是 JSONArray。所以改变你的解析逻辑

JSONObject pages = query.getJSONObject("pages");
于 2016-06-02T07:38:09.177 回答
1

错误,JSONArray pages = query.getJSONArray("pages"); 因为“页面”是 JSONObject。将您的代码替换为:

JSONObject resultJSON = new JSONObject(result);
JSONObject query = resultJSON.getJSONObject("query");  

JSONObject pages = query.getJSONObject("pages");  

  for (int i = 0; i < pages.length(); i++) {
        JSONObject j1 = pages.getJSONObject(i);
        Log.e("title", j1.getString("title"));
            if (j1.has("thumbnail")) {
                JSONObject thumbnail = j1.getJSONObject("thumbnail");
                 Log.e("source", thumbnail.getString("source"));
             } else {
                      Log.e("not found", "thumbnail");
                 }
      }  
于 2016-06-02T07:41:28.117 回答