我有这个Google Books JSON文本要解析:
"volumeInfo": {
"title": "Year Book",
"subtitle": "The Annual Supplement to the World Book Encyclopedia : the 1989 World Book : a Review of the Events of 1988",
"authors": [
"World Book Encyclopedia"
],
"publishedDate": "1989",
"industryIdentifiers": [
{
"type": "ISBN_10",
"identifier": "0716604892"
},
{
"type": "ISBN_13",
"identifier": "9780716604891"
}
],
"readingModes": {
"text": false,
"image": false
},
"pageCount": 608,
"printType": "BOOK",
"categories": [
"Encyclopedias and dictionaries"
],
"maturityRating": "NOT_MATURE",
"allowAnonLogging": false,
"contentVersion": "0.1.1.0.preview.0",
"panelizationSummary": {
"containsEpubBubbles": false,
"containsImageBubbles": false
},
"imageLinks": {
"smallThumbnail": "http://books.google.com/books/content?id=SDFIuwEACAAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api",
"thumbnail": "http://books.google.com/books/content?id=SDFIuwEACAAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api"
},
"language": "en",
"previewLink": "http://books.google.it/books?id=SDFIuwEACAAJ&dq=isbn:0716604892&hl=&cd=2&source=gbs_api",
"infoLink": "http://books.google.it/books?id=SDFIuwEACAAJ&dq=isbn:0716604892&hl=&source=gbs_api",
"canonicalVolumeLink": "https://books.google.com/books/about/Year_Book.html?hl=&id=SDFIuwEACAAJ"
}我需要深入了解以下内容:
"industryIdentifiers": [
{
"type": "ISBN_10",
"identifier": "0716604892"
},
{
"type": "ISBN_13",
"identifier": "9780716604891"
}
]但我的代码是:
String ISBN = "null";
if(volumeInfo.toString().contains("industryIdentifiers")) {
JSONArray industryIdentifiers = volumeInfo.getJSONArray("industryIdentifiers");
if(industryIdentifiers.length() == 1){
industryIdentifiers = volumeInfo.getJSONObject("industryIdentifiers").getJSONArray("0");
ISBN = industryIdentifiers.getString(0 ) + ": " + industryIdentifiers.getString(1);
}
else if (industryIdentifiers.length() == 2) {
industryIdentifiers = volumeInfo.getJSONObject("industryIdentifiers").getJSONArray("0");
ISBN = industryIdentifiers.getString(0 ) + ": " + industryIdentifiers.getString(1);
industryIdentifiers = volumeInfo.getJSONObject("industryIdentifiers").getJSONArray("1");
ISBN = ISBN + " - " + industryIdentifiers.getString(0 ) + ": " + industryIdentifiers.getString(1);
}
}
else{ISBN = "null";}在调试器(应用程序冻结)中产生此结果。请注意:结果是在一个循环中,你应该只考虑第一行。
E/: Value [{"type":"ISBN_13","identifier":"9788830441392"},{"type":"ISBN_10","identifier":"8830441392"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830439719"},{"type":"ISBN_10","identifier":"8830439711"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830439696"},{"type":"ISBN_10","identifier":"883043969X"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
Value [{"type":"ISBN_13","identifier":"9788830447110"},{"type":"ISBN_10","identifier":"8830447110"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830453159"},{"type":"ISBN_10","identifier":"8830453153"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830439634"},{"type":"ISBN_10","identifier":"8830439630"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830428454"},{"type":"ISBN_10","identifier":"8830428450"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830439658"},{"type":"ISBN_10","identifier":"8830439657"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830449770"},{"type":"ISBN_10","identifier":"8830449776"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830442856"},{"type":"ISBN_10","identifier":"8830442852"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830449480"},{"type":"ISBN_10","identifier":"8830449482"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830441408"},{"type":"ISBN_10","identifier":"8830441406"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
Value [{"type":"ISBN_13","identifier":"9788830453807"},{"type":"ISBN_10","identifier":"8830453803"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830439610"},{"type":"ISBN_10","identifier":"8830439614"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830441460"},{"type":"ISBN_10","identifier":"8830441465"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830442702"},{"type":"ISBN_10","identifier":"8830442704"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
Value [{"type":"ISBN_13","identifier":"9788830444997"},{"type":"ISBN_10","identifier":"8830444995"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830448704"},{"type":"ISBN_10","identifier":"8830448702"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830443877"},{"type":"ISBN_10","identifier":"8830443875"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830441415"},{"type":"ISBN_10","identifier":"8830441414"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject我的代码基于这个公认的答案:How to access nested elements of json object using getJSONArray method
如果可能的话,我不想使用库。
如你所见,我的逻辑是基于我在浏览器上看到的:

发布于 2019-10-19 09:34:37
industryIdentifiers是一个对象数组,因此首先调用getJSONArray(String name)(https://developer.android.com/reference/org/json/JSONObject.html#getJSONArray(java.lang.String%29)来获取数组,然后调用getJSONObject(int index)(https://developer.android.com/reference/org/json/JSONArray.html#getJSONObject(int%29)来获取对象。
你的代码应该是:
JSONArray industryIdentifiers = volumeInfo.getJSONArray("industryIdentifiers");
if (industryIdentifiers.length() == 1) {
JSONObject obj = industryIdentifiers.getJSONObject(0);
ISBN = obj.getString("type") + ": " + obj.getString("identifier");
} else if (industryIdentifiers.length() == 2) {
JSONObject obj1 = industryIdentifiers.getJSONObject(0);
JSONObject obj2 = industryIdentifiers.getJSONObject(1);
ISBN = obj1.getString("type") + ": " + obj1.getString("identifier") + " - "
+ obj2.getString("type") + ": " + obj2.getString("identifier");
}发布于 2019-10-19 09:39:11
这是我将采取的方法:
if(volumeInfo.toString().contains("industryIdentifiers")) {
JSONArray industryIdentifiers = volumeInfo.getJSONArray("industryIdentifiers");
if(industryIdentifiers != null && industryIdentifiers.length > 0) {
for(int i = 0; i < industryIdentifiers.length; i++) {
JSONObject industryIdentifier = industryIdentifiers.getJSONObject(i);
// Get the ISBN info from the current identifier and concatenate it to your ISBN string
}
} else {
ISBN = "null";
}发布于 2019-10-19 10:03:00
融合了这两个答案,完美的工作解决方案,满足我的需求:
//ISBN
String ISBN = "";
if(volumeInfo.toString().contains("industryIdentifiers")) {
JSONArray industryIdentifiers = volumeInfo.getJSONArray("industryIdentifiers");
if (industryIdentifiers.length() > 0) {
for(int counter = 0; counter < industryIdentifiers.length(); counter++){
JSONObject obj = industryIdentifiers.getJSONObject(counter);
ISBN = ISBN + obj.getString("type") + ": " + obj.getString("identifier") + " ";
}
}
}
else{ISBN = "null";}https://stackoverflow.com/questions/58459729
复制相似问题