Gson – How to parse JSON Arrays, an array of arrays

json array

In this article, we will show few JSON array examples and also how to use Gson to map it back to Java Object.

pom.xml

  <!-- Gson JSON -> Object -->
  <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.6</version>
  </dependency>

JSON basic.

  • [ ] = array
  • { } = object

1. JSON array of object.

1.1 Sample.

array1.json

[
  {
    "id": 1, "name": "a"
  },
  {
    "id": 2, "name": "b"
  }
]

1.2 Gson convert above JSON array into a List<Item>.

Item.java

package com.mkyong;

public class Item {

    private int id;
    private String name;

}
JsonArray1.java

package com.mkyong;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class JsonArray1 {

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

        JavaParseJsonArray main = new JavaParseJsonArray();

        Gson gson = new Gson();

        Type listType = new TypeToken<List<Item>>() {}.getType();

        List<Item> list = gson.fromJson(main.loadFileFromClasspath("array1.json"), listType);

        System.out.println(gson.toJson(list));

    }

    public String loadFileFromClasspath(String fileName) throws IOException {
        ClassLoader classLoader = getClass().getClassLoader();
        try (InputStream inputStream = classLoader.getResourceAsStream(fileName)) {
            // common-io
            return IOUtils.toString(inputStream, StandardCharsets.UTF_8);
        }
    }

}

P.S We use common-io to convert inputStream to String.

pom.xml

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>

2. JSON array of {object with an array of object}.

2.1 Sample.

array2.json

[
  {
    "id": 1,
    "name": "a",
    "types":
    [
      {"id": 1,"name": "a1"},
      {"id": 2,"name": "a2"}
    ]
  },
  {
    "id": 2,
    "name": "b",
    "types":
    [
      {"id": 1,"name": "b1"},
      {"id": 2,"name": "b2"}
    ]
  }
]

2.2 Gson convert above JSON array into a List<Item> containing List<ItemType> types.

Item.java

package com.mkyong;

import java.util.List;

public class Item {

    private int id;
    private String name;
    private List<ItemType> types;

}
ItemType.java

package com.mkyong;

public class ItemType {

    private int id;
    private String name;

}

2.3 Same code, no need to change.


  Type listType = new TypeToken<List<Item>>() {}.getType();
  List<Item> list = gson.fromJson(main.loadFileFromClasspath("array2.json"), listType);

3. JSON array of {object with an array of an array of object}.

3.1 Sample.

array3.json

[
  {
    "id": 1,
    "name": "a",
    "types":
    [
      [
        {"id": 1,"name": "a1"},
        {"id": 2,"name": "a2"}
      ],
      [
        {"id": 3,"name": "a3"}
      ]
    ]
  },
  {
    "id": 2,
    "name": "b",
    "types":
    [
      [
        {"id": 1,"name": "b1"}
      ],
      [
        {"id": 2,"name": "b2"}
      ]
    ]
  }
]

3.2 Change the types to List<ItemType> types[];

Item.java

package com.mkyong;

import java.util.List;

public class Item {

    private int id;
    private String name;
    private List<ItemType> types[]; // change types -> types[]

}
ItemType.java

package com.mkyong;

public class ItemType {

    private int id;
    private String name;

}

3.3 Same code, no need to change.


  Type listType = new TypeToken<List<Item>>() {}.getType();
  List<Item> list = gson.fromJson(main.loadFileFromClasspath("array3.json"), listType);

3.4 See the debugging mode.

ideaj debug

4. JSON array of {object with an array of an array of String}.

4.1 This is a bit unusual JSON array sample.

array4.json

[
  {
    "id": 1,
    "name": "a",
    "types":
    [
      [
        "a1", 1
      ],
      [
        "a2", 2
      ]
    ]
  },
  {
    "id": 2,
    "name": "b",
    "types":
    [
      [
        "b1", 1
      ]
    ]
  }
]

4.2 Change the types to List<String> types[];

Item.java

package com.mkyong;

import java.util.List;

public class Item {

    private int id;
    private String name;
    private List<String> types[];

}

4.3 Same code, no need to change.


  Type listType = new TypeToken<List<Item>>() {}.getType();
  List<Item> list = gson.fromJson(main.loadFileFromClasspath("array4.json"), listType);

References

author image

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities. Read all published posts by

Comments

avatar