我正在使用维基百科API,它将结果返回到JSON数组
API:
{"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}}}}
响应:
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");
}
}
我想从“pages”数组中获取“title”和“thumbnail”“source”值。
这是我解析响应的代码
at org.json.JSON.typeMismatch(JSON.java:100)
at org.json.JSONObject.getJSONArray(JSONObject.java:553)
异常
describe( 'Compile: ', function () {
it( 'compare two html strings and should be equal', function () {
var mockEl = $( '<i class="material-icons"></i>' )
;
expect( mockEl[0].outerHTML ).toBe(
'<i class="material-icons"></i>'
);
} );
} );
答案 0 :(得分:2)
同意@AndhanHM在评论中提到的内容。 page
是JSONObject
,而不是JSONArray
。为了迭代特定的JSONObject
,我可能会使用这样的answer by 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);
}
干杯!
答案 1 :(得分:1)
在您的代码中,“pages”是JSONObject而不是JSONArray。所以你要解析逻辑
JSONObject pages = query.getJSONObject("pages");
答案 2 :(得分:1)
JSONArray pages = query.getJSONArray("pages");
中的错误,因为'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");
}
}