1. Introduction
JSON, short for JavaScript Object Notation, serves as a common method for transmitting data between servers and clients in web development. Its versatility and simplicity make it a go-to choice for developers worldwide.
Occasionally, there arises a need to convert a JSON array to List object, enhancing the data’s accessibility and facilitating smoother processing in subsequent stages of development.
In this tutorial, we will explore various methods for accomplishing this conversion, comparing the techniques offered by two widely-used JSON libraries in Java – Gson and Jackson.
2. Using Gson Library
Gson, a popular JSON library, is extensively utilized for converting Java objects to JSON (JavaScript Object Notation) and vice versa through serialization and deserialization processes. It provides an easy way to transform a JSON array to List object.
2.1. Adding Gson Maven Dependency
To incorporate Gson into our project, we must include the Gson library as a dependency:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
2.2. Converting JSON Array to List
In this section, we’ll explore how to convert a JSON array to List using the Gson library.
Imagine, if we have a bunch of electronic devices listed in a JSON format. Here’s how it might look:
[
{"id":101, "name":"Laptop", "description":"Portable computing device"},
{"id":102, "name":"Smartphone", "description":"Mobile communication device"},
{"id":103, "name":"Headphones", "description":"Audio output devices"}
]
The above JSON array represents a list of electronic devices:
public class ElectronicDevice {
private int id;
private String name;
private String description;
public ElectronicDevice(int id, String name, String description) {
this.id = id;
this.name = name;
this.description = description;
}
// getters and setters
}
Now, let’s understand how to convert this JSON array into a List:
@Test
public void whenUsingGsonLibrary_thenCompareTwoDevices() {
Gson gson = new Gson();
Type listType = new TypeToken<List<ElectronicDevice>>() {}.getType();
List<ElectronicDevice> deviceList = gson.fromJson(jsonArray, listType);
Assert.assertEquals(101, deviceList.get(0).getId());
Assert.assertEquals("Portable computing device", deviceList.get(0).getDescription());
Assert.assertEquals("Laptop", deviceList.get(0).getName());
}
First, create an instance of the Gson class, which provides methods for JSON serialization and deserialization.
Specify the target List type using the TypeToken class. In this example, we’ve defined the target type as List<ElectronicDevice>.
Then, use the fromJson() method of the Gson object to convert the JSON array String to a List.
After converting the JSON array to List, let’s analyze the assertions.
Here, we’re checking details like ID or description in the JSON data against a list of electronic devices ‘deviceList‘, representing a List of ElectronicDevice objects.
3. Using the Jackson Library
Jackson is also a popular JSON library for Java. In this section, we will explore how to convert a JSON array to List in Java using the Jackson library.
3.1. Jackson Maven Dependency
To utilize the Jackson library in our project, we must include the following dependency in the project’s configuration:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.2</version>
</dependency>
3.2. Converting JSON Array to Java List
In this section, we’ll explore how to convert a JSON array into a List using Jackson:
@Test
public void whenUsingJacksonLibrary_thenCompareTwoDevices() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
TypeReference<List<ElectronicDevice>> listType = new TypeReference<List<ElectronicDevice>>() {};
List<ElectronicDevice> jacksonList = objectMapper.readValue(jsonArray, listType);
Assert.assertEquals(101, jacksonList.get(0).getId());
Assert.assertEquals("Portable computing device", jacksonList.get(0).getDescription());
Assert.assertEquals("Laptop", jacksonList.get(0).getName());
}
We create an instance of the ObjectMapper class, which is the central class in the Jackson library for handling data.
The TypeReference class allows us to specify the type of the target List. In this example, we’ve specified the target type as List<ElectronicDevice>.
Next, we utilize the readValue() method of the ObjectMapper object to convert the JSON array String into a List.
Similar to the earlier discussed assertion, we proceed to compare specific fields from the JSON array String to the corresponding fields in the jacksonList.
4. Conclusion
In this article, we’ve examined the process of converting a JSON array to a Java List using two widely used libraries: Gson and Jackson.
Gson offers a simple method, whereas Jackson offers advanced features and superior performance. The decision between Gson and Jackson relies on the specific needs and preferences of the project.