Accessing json in tutorial# 12 from "Busy"

from the CommonsWare Community archives

At February 27, 2018, 10:06pm, dhowell asked:

So, I’m a beginner (old beginner, I was a programmer in the 80’s and trying to refresh), and I was simply trying to figure out how to retrieve the book title from the json. It looks like it should be simple but for the life of me, I could not do it. I’m a little sketchy on arraylists.

In the conext of that tutorial, how do I retrieve the book title from that json file?

We see methods there for chapter title and etc which make sense (kind of :slight_smile: but I want the book title, which eludes me.

I decided to stop cursing, nurse the bumps on my head from the wall banging, step on my ego and just ask.

I know its a stupid question, I can FEEL it, but I just seem to have a block.

Thanks,

Dan


At February 27, 2018, 10:25pm, mmurphy replied:

TL;DR: I’m not using the title top-level JSON property, so there is no code for that.

Here is the code where I am parsing the JSON, using Gson:

      Gson gson=new Gson();

      try {
        InputStream is=assets.open("book/contents.json");
        BufferedReader reader=
          new BufferedReader(new InputStreamReader(is));

        contents.set(gson.fromJson(reader, BookContents.class));

        EventBus.getDefault().post(new BookLoadedEvent(getBook()));
      }
      catch (IOException e) {
        Log.e(getClass().getSimpleName(), "Exception parsing JSON", e);
      }

Gson’s object mapping API takes the source of the JSON (reader) and the Java class that you want to pour the JSON into (BookContents). Gson parses the JSON, creates an instance of your targeted class, attempts to match the JSON bits into the class’ fields, and returns the instance of your class.

In this case, it is mapping into BookContents, which is set up to mimic the JSON structure:

public class BookContents {
  List<BookContents.Chapter> chapters;

  int getChapterCount() {
    return(chapters.size());
  }

  String getChapterFile(int position) {
    return(chapters.get(position).file);
  }

  String getChapterTitle(int position) {
    return(chapters.get(position).title);
  }

  static class Chapter {
    String file;
    String title;
  }
}

The JSON and the Java class do not have to completely match, though. In this case, the JSON contains a top-level title field, in addition to the chapters:

{
  "title": "The War of the Worlds",
  "chapters": [
    {
      "file": "0.htm",
      "title": "Book One: Chapters 1-9"
    },
    {
      "file": "1.htm",
      "title": "Book One: Chapters 10-14"
    },
    {
      "file": "2.htm",
      "title": "Book One: Chapters 14-17"
    },
    {
      "file": "3.htm",
      "title": "Book Two: Chapters 1-7"
    },
    {
      "file": "4.htm",
      "title": "Book Two: Chapters 7-10"
    },
    {
      "file": "5.htm",
      "title": "Project Gutenberg"
    }
  ]
}

In my case, I did not need that data (though I may have in some long-ago edition of the tutorials). So, BookContents does not have a field for it.

But, you could add a String title field to BookContents, and Gson would happily populate it. You could then use that field as you see fit.

And, if I misunderstood your question, I apologize!