duy anh hoang
duy anh hoang

Reputation: 195

extracting value from a JSONArray in Android

I have this JSON Object

{
"kind": "books#volumes",
 "totalItems": 482,
 "items": [
  {
   "kind": "books#volume",
   "id": "MoXpe6H2B5gC",
   "etag": "6dr4Ka3Iksc",
   "selfLink": "https://www.googleapis.com/books/v1/volumes/MoXpe6H2B5gC",
   "volumeInfo": {
    "title": "Android in The Attic",
    "authors": [
     "Nicholas Allan"
    ],
    "publisher": "Hachette UK",
    "publishedDate": "2013-01-03",
    "description": "Aunt Edna has created a no-nonsense nanny android to make sure Billy and Alfie don't have any fun. But then Alfie discovers how to override Auntie Anne-Droid's programming and nothing can stop them eating all the Cheeki Choko Cherry Cakes they like ... until the real aunt Edna is kidnapped!",

I have to extract 3 keys "title", "author", and "description" by this code snippet:

JSONObject baseJsonResponse = new JSONObject(bookJSON);

        // Extract the JSONArray associated with the key called "features",
        // which represents a list of features (or books).
        JSONArray bookArray = baseJsonResponse.getJSONArray("items");

        // For each book in the bookArray, create an {@link book} object
        for (int i = 0; i < bookArray.length(); i++) {

            // Get a single book at position i within the list of books
            JSONObject currentBook = bookArray.getJSONObject(i);

            // For a given book, extract the JSONObject associated with the
            // key called "volumeInfo", which represents a list of all volumeInfo
            // for that book.
            JSONObject volumeInfo = currentBook.getJSONObject("volumeInfo");

            // Extract the value for the key called "title"
            String title = volumeInfo.getString("title");

            // Extract the value for the key called "authors"
            String authors = volumeInfo.getString("author");

            // Extract the value for the key called "description"
            String description = volumeInfo.getString("description");

The "title" and "description" worked fine, but the author part didn't. As I can see, "author" is actually an JSONArray, so my output on screen is

["Nicholas Allan"]

which is not what I desired. So, I tried to change my approach and extract the element by this code

JSONArray author = volumeInfo.getJSONArray("authors");
                String authors = author.get(0);

But Android Studio said the input of method get() must be a String. I'm new to JSON and Android so I have never seen a JSON key without a value like this one. Can anyone tell me how can I extract an element from a JSONArray?

Upvotes: 1

Views: 3472

Answers (1)

Eric Bachhuber
Eric Bachhuber

Reputation: 3952

Since the get() method returns an Object, you would need to cast it to a String:

String authors = (String) author.get(0);

Alternatively, you could use JSONArray's getString(index) method, where 0 is the index.

JSONArray author = volumeInfo.getJSONArray("authors");
String authors = author.getString(0);

Upvotes: 4

Related Questions