gpt4 book ai didi

java - JSON解析子项而不使用任何库

转载 作者:行者123 更新时间:2023-12-01 19:34:08 25 4
gpt4 key购买 nike

我有这个 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

如果可能的话,我不想使用库。

如您所见,我的逻辑基于我在浏览器上看到的内容: Screenshot

最佳答案

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");
}

关于java - JSON解析子项而不使用任何库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58459729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com