Search code examples
javaarraysapiintellij-ideajson-simple

Accessing the inner array's values in a JSONArray


I am using IntelliJ and the Gooks Books API and I'm coding in Java while using json simple. I am writing a program where the user can type a book title in terminal and the API returns 5 books and information on each of those books. The API returns a lot of information on the books but I only want it to return the book title, author, and publisher. I'm new to using APis and JSON so I don't know if my errors can be from syntax or logic errors.

For example, I type in Harry Potter and this is part of what the API returns

{
 "kind": "books#volumes",
 "totalItems": 1832,
 "items": [
  {
   "kind": "books#volume",
   "id": "cvRFvgAACAAJ",
   "etag": "mfbVxWEkveo",
   "selfLink": "https://www.googleapis.com/books/v1/volumes/cvRFvgAACAAJ",
   "volumeInfo": {
    "title": "Harry Potter and the Cursed Child",
    "subtitle": "Parts one and two",
    "authors": [
     "Jack Thorne",
     "J. K. Rowling",
     "John Tiffany"
    ],
    "publisher": "THORNDIKE Press",
    "publishedDate": "2016",
    "description": "\"Thorndike Press large print the literacy bridge.\"",
    "industryIdentifiers": [
     {
      "type": "ISBN_10",
      "identifier": "1410496201"
     },
     {
      "type": "ISBN_13",
      "identifier": "9781410496201"
     }
    ],
    "readingModes": {
     "text": false,
     "image": false
    },
    "pageCount": 407,
    "printType": "BOOK",
    "categories": [
     "Good and evil"
    ],

I've read that one of the ways to retrieve specific information is to create a JSONArray and make the information from the API be JSONObjects. I was successfully able to parse the data so each JSONObject is one book and all it's information, but I don't know how to ONLY print out the specific information I want.

This is some of the code I have. The code from TODO onward is where I'm having trouble

String inline = ""; //gets the JSON data and makes it a String
    Scanner sc = new Scanner(url.openStream()); //reads JSON data
    while (sc.hasNext())
    {
      inline += sc.nextLine();
    }
    sc.close();

    JSONParser parse = new JSONParser();
    JSONObject jObj = (JSONObject)parse.parse(inline);
    JSONArray theJArray = (JSONArray)jObj.get("items");

    for (int i = 0; i < theJArray.size(); i++)
    {
      JSONObject secondJObj = (JSONObject)theJArray.get(i);
      System.out.println("The secondJobj: " + secondJObj); //one object is one book and its info

      JSONArray specificArr = (JSONArray)secondJObj.get("author"); //TODO
      System.out.println("The specificArr is: " + specificArr); //returns null
      System.out.println("The specific section: " + secondJObj.get("author")); //returns null
    }

From my understanding, everything is under the "items" array, so the "authors" array is an array inside another array. How do I access the information in the "authors" array?

I also need the title and publisher of the book and I know those aren't arrays so it's unlike calling for the information from the "authors" array. How do I retrieve the information from the "items" array just for the values for the keys "title" and "publisher"?


Solution

  • I can see your effort in the above code, But author is inside of volumeInfo JsonObject

    "volumeInfo": {
    "title": "Harry Potter and the Cursed Child",
    "subtitle": "Parts one and two",
    "authors": [
     "Jack Thorne",
     "J. K. Rowling",
     "John Tiffany"
    ],
    

    So in the for loop first you need to get volumeInfo as JSONObject and then get author as JSONArray

    for (int i = 0; i < theJArray.size(); i++)
    {
      JSONObject secondJObj = (JSONObject)theJArray.get(i);
      System.out.println("The secondJobj: " + secondJObj); //one object is one book and its info
    
      JSONObject volInfo = (JSONObject)secondJObj.get("volumeInfo"); //TODO
        // then get author
       JSONArray specificArr = (JSONArray)volInfo.get("author");
    }