3 回答

TA貢獻1906條經(jīng)驗 獲得超3個贊
industryIdentifiers
是一個對象數(shù)組,因此您首先調(diào)用getJSONArray(String name)
以獲取數(shù)組,然后調(diào)用getJSONObject(int index)
以獲取對象。
你的代碼應該是:
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");
}

TA貢獻1845條經(jīng)驗 獲得超8個贊
合并兩個答案,滿足我的需求的完美工作解決方案:
//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";}

TA貢獻1804條經(jīng)驗 獲得超3個贊
這是我將采取的方法:
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";
}
添加回答
舉報