2016-06-02 19 views
-2

ich die wikipedia API verwende, gibt es Ergebnis in einem JSON-ArrayParsen von JSON in Android, typeMismatch Fehler?

API:

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 

Antwort:

{"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}}}} 

ich den "Titel" und "Miniatur" erhalten möchten "source" -Werte aus dem Array "pages".

Das ist mein Code ist die Antwort

zum Parsen
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"); 
       } 
     } 

Exception

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

Mögliche Duplikat von [Wie JSON in Android analysieren] (http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android) –

+2

JSONArray Seiten = query.getJSONArray ("pages"); ist Array nicht sein JSONObject –

Antwort

2

Zustimmung zu was @AndhanHM in den Kommentaren erwähnt. page ist ein JSONObject und kein JSONArray. Und iterieren diese spezifischen JSONObject, würde ich wahrscheinlich mit so etwas wie dieser answer by mtariq gehen:

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

Prost!

+0

@WISHY hat es funktioniert? Es wäre am besten, wenn Sie eine der Antworten akzeptieren, sodass Ihr Beitrag ordnungsgemäß als "Beantwortet" markiert wird. Prost! :) –

1

In Ihrem Code "Seiten" ist JSONObject nicht JSONArray. So changege Sie Parsing Logik zu

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

Fehler in JSONArray pages = query.getJSONArray("pages");, da 'Seiten' JSONObject ist. Ersetzen Sie den Code mit:

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"); 
       } 
     }