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

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

     }

    ]




慕姐4208626
浏览 79回答 3
3回答

一只名叫tom的猫

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

精慕HU

合并两个答案,满足我的需求的完美工作解决方案://ISBNString ISBN = "";&nbsp; &nbsp; &nbsp;if(volumeInfo.toString().contains("industryIdentifiers")) {&nbsp; &nbsp; &nbsp;JSONArray industryIdentifiers = volumeInfo.getJSONArray("industryIdentifiers");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (industryIdentifiers.length() > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int counter = 0; counter < industryIdentifiers.length(); counter++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject obj = industryIdentifiers.getJSONObject(counter);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ISBN = ISBN + obj.getString("type") + ": " + obj.getString("identifier") + " ";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;}}else{ISBN = "null";}

狐的传说

这是我将采取的方法:if(volumeInfo.toString().contains("industryIdentifiers")) {&nbsp; &nbsp;JSONArray industryIdentifiers = volumeInfo.getJSONArray("industryIdentifiers");if(industryIdentifiers != null && industryIdentifiers.length > 0) {&nbsp; for(int i = 0; i < industryIdentifiers.length; i++) {&nbsp; &nbsp;JSONObject industryIdentifier = industryIdentifiers.getJSONObject(i);// Get the ISBN info from the current identifier and concatenate it to your ISBN string}} else {&nbsp; ISBN = "null";}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java