为了账号安全,请及时绑定邮箱和手机立即绑定

JSON 解析子项而不使用任何 lib

JSON 解析子项而不使用任何 lib

慕姐4208626 2023-11-10 15:27:52
我有这个 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"     }    ]
查看完整描述

3 回答

?
一只名叫tom的猫

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

}


查看完整回答
反对 回复 2023-11-10
?
精慕HU

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


查看完整回答
反对 回复 2023-11-10
?
狐的传说

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

}


查看完整回答
反对 回复 2023-11-10
  • 3 回答
  • 0 关注
  • 125 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信