JSON to Array, JSON to List in Java

Array/List in JSON

Let’s assume we have the JSON that presents a list or array of books.

{
  "title": "War and Peace",
  "author": "L Tolstoy",
  "numberOfPages": 700,
  "chapterTitles": [
    "Chapter1",
    "Chapter2",
    "Chapter3",
    "Chapter4"
  ]
}
{
  "title": "Yevgeny Onegin",
  "author": "A Pushkin",
  "numberOfPages": 500,
  "chapterTitles": [
    "Chapter #1",
    "Chapter #2",
    "Chapter #3"
  ]
}
{
  "title": "A Hero of Our Time",
  "author": "M Lermontov",
  "numberOfPages": 200,
  "chapterTitles": [
    "Chapter 1",
    "Chapter 2"
  ]
}

We need to convert that JSON to a Java object with GSON (GSON Array or GSON List).

The regular approach described in JSON with GSON works as well

We can use the class we defined in Print Java objects in JSON format. Pay attention toString() method.

    public class Book {
        public String title;
        public String author;
        public int numberOfPages;
        public List<String> chapterTitles;

        public String toString() {
            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            return gson.toJson(this);
        }
    }

JSON to Array. GSON array

    Gson gson = new Gson();
    Book[] myArray = gson.fromJson(json, Book[].class);

JSON to List. GSON list

    Gson gson = new Gson();
    Book[] myArray = gson.fromJson(json, Book[].class);
    List<Book> myList = Arrays.asList(myArray);

Complete example JSON to List and JSON to Array

Real Java code with GSON List and GSON Array can look

    public static void main(String[] args) throws Exception {

        System.out.println("Result: " + reverseString(null));
        System.out.println("Result: " + fizzBuzz(0));

        String json =
                        "[{   " +
                            "'title': 'War and Peace',   " +
                            "'author': 'L Tolstoy',   " +
                            "'numberOfPages': 700,   " +
                            "'chapterTitles': ['Chapter1', 'Chapter2', 'Chapter3', 'Chapter4']" +
                        "}," +
                        "{   " +
                            "'title': 'Yevgeny Onegin',   " +
                            "'author': 'A Pushkin',   " +
                            "'numberOfPages': 500,   " +
                            "'chapterTitles': ['Chapter #1', 'Chapter #2', 'Chapter #3']" +
                        "}," +
                        "{   " +
                            "'title': 'A Hero of Our Time',   " +
                            "'author': 'M Lermontov',   " +
                            "'numberOfPages': 200,   " +
                            "'chapterTitles': ['Chapter 1', 'Chapter 2']" +
                        "}]";

        Gson gson = new Gson();

//        Array
        Book[] myArray = gson.fromJson(json, Book[].class);
        System.out.println("Array of Books");
        for (Book book : myArray) {
            System.out.println(book);
        }
        System.out.println();

//        List
        List<Book> myList = Arrays.asList(myArray);
        System.out.println("List of Books");
        for (Book book : myList) {
            System.out.println(book);
        }
    }

    public class Book {
        public String title;
        public String author;
        public int numberOfPages;
        public List<String> chapterTitles;

        public String toString() {
            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            return gson.toJson(this);
        }
    }

Here we converted a string in JSON format which is just a list of books to Java Array and Java List with GSON. Then we converted them back to JSON. Execution gives us that console output

Array of Books
{
  "title": "War and Peace",
  "author": "L Tolstoy",
  "numberOfPages": 700,
  "chapterTitles": [
    "Chapter1",
    "Chapter2",
    "Chapter3",
    "Chapter4"
  ]
}
{
  "title": "Yevgeny Onegin",
  "author": "A Pushkin",
  "numberOfPages": 500,
  "chapterTitles": [
    "Chapter #1",
    "Chapter #2",
    "Chapter #3"
  ]
}
{
  "title": "A Hero of Our Time",
  "author": "M Lermontov",
  "numberOfPages": 200,
  "chapterTitles": [
    "Chapter 1",
    "Chapter 2"
  ]
}

List of Books
{
  "title": "War and Peace",
  "author": "L Tolstoy",
  "numberOfPages": 700,
  "chapterTitles": [
    "Chapter1",
    "Chapter2",
    "Chapter3",
    "Chapter4"
  ]
}
{
  "title": "Yevgeny Onegin",
  "author": "A Pushkin",
  "numberOfPages": 500,
  "chapterTitles": [
    "Chapter #1",
    "Chapter #2",
    "Chapter #3"
  ]
}
{
  "title": "A Hero of Our Time",
  "author": "M Lermontov",
  "numberOfPages": 200,
  "chapterTitles": [
    "Chapter 1",
    "Chapter 2"
  ]
}

You may also find these posts interesting: