0

I have this Google Books JSON text to parse:

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

In depth I need to catch this:

 "industryIdentifiers": [
     {
      "type": "ISBN_10",
      "identifier": "0716604892"
     },
     {
      "type": "ISBN_13",
      "identifier": "9780716604891"
     }
    ]

But my code:

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

Produces this result in debugger (the app freeze). Please note: The result is in a loop, you should consider the first line only.

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

My code is based on this accepted answer: How to access nested elements of json object using getJSONArray method

If possible I want to not to use a library.

As you can see my logic is based on what I see on the browser: Screenshot

WizardingStudios
  • 554
  • 2
  • 7
  • 22
  • Why do you first call `volumeInfo.getJSONArray("industryIdentifiers")`, then call `volumeInfo.getJSONObject("industryIdentifiers")`? You already know it's an *array*, so why is second call for an *object*? Why even have the second call, given that you assigned value of first call to a local variable? Just use the variable, don't make the call again, and certainly don't make another *incorrect* call. – Andreas Oct 19 '19 at 01:27
  • I created industryIdentifiers once. First time I'm using it to get array dimension. After this I simple use it again to get both results. I'm confused, please should you show me a code please? – WizardingStudios Oct 19 '19 at 01:35

3 Answers3

3

industryIdentifiers is an array of objects, so you first call getJSONArray(String name) to get the array, then call getJSONObject(int index) to get the objects.

Your code should be:

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");
}
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • This is correct. Same approach as mine. I just iterate and concatenate the ISBN in a for loop since that would account for the case of there being more than 2 ISBN info. – Jarvis Oct 19 '19 at 01:41
1

This is the approach I would take:

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";
}
Jarvis
  • 392
  • 1
  • 6
1

Merging both answer, perfect working solution for my needs:

//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";}
WizardingStudios
  • 554
  • 2
  • 7
  • 22