Search code examples
androidjsonparsinggoogle-books

Parsing JSON : Googlebooks JSON file getting specific identifier


I'm writing an application that queries Google Books and it will parse the JSON file and display the title and author and the ISBN_10 identifier of the book. For example I'm trying to parse the JSON file from the following link. What I had achieved so far is getting the title and author of the book which is fine. One of the main thing I wanted to do is to get the ISBN 10 number which in this case is "1558607129". So far using my current code it returns the following result:

{"type":"ISBN_10", "identifier":"1558607129"}
{"type":"ISBN_13", "identifier":"9781558607125"}

The above result shows that the function parsed everything within the "industryIdentifiers" JSON array which I do not want. I only want "1558607129".

Here is the function for parsing the JSON so far:

public void parseJson(String stringFromInputS)
{
    try 
    {
        JSONObject jsonObject= new JSONObject(stringFromInputS);

        JSONArray jArray = jsonObject.getJSONArray("items");
        for(int i = 0; i < jArray.length(); i++)
        {
            JSONObject jsonVolInfo = jArray.getJSONObject(i).getJSONObject("volumeInfo");
            String bTitle = jsonVolInfo.getString("title");

            JSONArray bookAuthors = jsonVolInfo.getJSONArray("authors");
            for(int j = 0; j < bookAuthors.length(); j++)
            {
                String bAuthor = bookAuthors.getString(i);
            }

            JSONArray jsonIndustrialIDArray = jsonVolInfo.getJSONArray("industryIdentifiers");
            for(int k = 0; k < jsonIndustrialIDArray.length(); k++)
            {
               String isbn10 = isbn10 + "\n" + jsonIndustrialIDArray.getString(k);
            }
       }
    }
}

So what I want to do is to grab specifically the ISBN_10 identifier and just that. And in this case it is "1558607129". I would like to know how to specify to just parse the isbn_10 number or if someone could point me to the right direction in doing so.

Thank you.


Solution

  • Maybe something like that?

    JSONArray jsonIndustrialIDArray = jsonVolInfo.getJSONArray("industryIdentifiers");
    for(int k = 0; k < jsonIndustrialIDArray.length(); k++) {
        JSONObject isbn = jsonIndustrialIDArray.getJSONObject(k);
        if (isbn.getString("type").equals("ISBN_10")) {
             String isbn10 = isbn.getString("identifier");
             break;
        }
    }