Json array to ArrayList gson

This example will demonstrate how to convert a json array to a java ArrayList using gson JSON parser. In the set up, we will create a sample json array to represent navigation elements and a java object to unmarshall into java object named NavItem. This is the same class used in the example how to marshal a json array to an array list using jackson.

Setup

Sample json array

[
  {
    "key":"Level up lunch",
    "url":"www.leveluplunch.com"
  },
  {
    "key":"Java examples",
    "url":"www.leveluplunch.com/java/examples/"
  },
  {
    "key":"Java exercises",
    "url":"www.leveluplunch.com/java/exercises/"
  },
  {
    "key":"Java tutorials",
    "url":"www.leveluplunch.com/java/tutorials/"
  }
]

Sample java class

class NavItem {

    private String linkText;
    private String url;

    ...
}

Serialization

Below we will serialize a java object into json using gson. We will initialize a java arraylist with two NavItems java objects by creating a Gson class from GSONBuilder class. The Gson class is the main class using Google Gson and can be built with various options. You will notice that we have commented out the ability to print json in a pretty format. Finally calling the toJson will serialize the navigation object into its equivalent Json representation.

@Test
public void convert_list_to_json() {

    NavItem item1 = new NavItem();
    item1.setKey("examples");
    item1.setUrl("http://leveluplunch.com/java/examples");

    NavItem item2 = new NavItem();
    item2.setKey("exercises");
    item2.setUrl("http://leveluplunch.com/java/exercises");

    List<NavItem> navigation = Lists.newArrayList(item1, item2);

    Gson gson = new GsonBuilder()
    // .setPrettyPrinting()
            .create();

    String arrayListToJson = gson.toJson(navigation);

    logger.info(arrayListToJson);

    assertEquals(
            "[{\"key\":\"examples\",\"url\":\"http://leveluplunch.com/java/examples\"},"
            + "{\"key\":\"exercises\",\"url\":\"http://leveluplunch.com/java/exercises\"}]",
            arrayListToJson);

}

Output

[
  {
    "key": "examples",
    "url": "http://leveluplunch.com/java/examples"
  },
  {
    "key": "exercises",
    "url": "http://leveluplunch.com/java/exercises"
  }
]

Deserialization

Converting from json to a java object or deserialization is a similar process. There is two subsnippets below. The first converts the json into an array of NavItems while the second deserializes a java generic type by specifying the correct parameterized type. You can do this by using the TypeToken class shown below.

@Test
public void convert_json_to_array_or_list() {

    Gson gson = new Gson();

    NavItem[] navigationArray = gson.fromJson(jsonString, NavItem[].class);

    logger.info(navigationArray);

    assertEquals(4, navigationArray.length);

    // or

    @SuppressWarnings("serial")
    Type collectionType = new TypeToken<List<NavItem>>() {
    }.getType();
    List<NavItem> navigation = gson.fromJson(jsonString, collectionType);

    logger.info(navigation);

    assertEquals(4, navigation.size());
}

Output

<[Lcom.levelup.java.json.JsonArrayToObjectArrayGson$NavItem;@63d4e2ba>
<[NavItem{key=Level up lunch, url=www.leveluplunch.com}, NavItem{key=Java examples, url=www.leveluplunch.com/java/examples/}, NavItem{key=Java exercises, url=www.leveluplunch.com/java/exercises/}, NavItem{key=Java tutorials, url=www.leveluplunch.com/java/tutorials/}]>