3 回答
TA贡献1906条经验 获得超3个赞
industryIdentifiers
是一个对象数组,因此您首先调用getJSONArray(String name)
以获取数组,然后调用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条经验 获得超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条经验 获得超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";
}
添加回答
举报