【发布时间】:2020-02-15 23:23:46
【问题描述】:
我有这个 Google 图书 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
如果可能,我不想使用库。
【问题讨论】:
-
为什么先打
volumeInfo.getJSONArray("industryIdentifiers"),再打volumeInfo.getJSONObject("industryIdentifiers")?你已经知道它是一个array,那么为什么第二次调用一个object呢?既然您将第一次调用的值分配给了局部变量,为什么还要进行第二次调用?只需使用变量,不要再调用,当然也不要再调用不正确的。 -
我创建了一次行业标识符。我第一次使用它来获取数组维度。在此之后,我再次简单地使用它来获得两个结果。我很困惑,请你给我一个代码吗?