Horje
How to Convert an ArrayList into a JSON String in Java?

In Java, an ArrayList is a resizable array implementation of the List interface. It implements the List interface and is the most commonly used implementation of List. In this article, we will learn how to convert an ArrayList into a JSON string in Java.

Steps to convert an ArrayList into a JSON string in Java

Below are the steps and implementation of converting an ArrayList into a JSON string with Jackson.

Step 1: Create a Maven Project

Open any preferred IDE and create a new Maven project. Here we are using IntelliJ IDEA, so, we can do this by selecting File -> New -> Project.. -> Maven and following the wizard.

Project Creation

Project Structure:

Below is the project structure that we have created.

Project Structure

project

Step 2: Add Jackson Dependency to pom.xml

Now, we will add Jackson dependency to pom.xml file.

XML

<?xml version="1.0" encoding="UTF-8"?>
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    <modelVersion>4.0.0</modelVersion>
  
    <groupId>org.example</groupId>
    <artifactId>ArrayList</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.12.5</version> <!-- Replace with the latest version -->
        </dependency>
    </dependencies>
    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
  
</project>

Step 3: Add logic of converting an ArrayList into a JSON string

Here we are adding the logic of converting an ArrayList into a JSON string of Java in the main class.

Java

package org.example;
  
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
  
public class Main {
    public static void main(String[] args)
    {
        // Create an ArrayList
        ArrayList<String> courses = new ArrayList<>();
        courses.add("Java");
        courses.add("Python");
        courses.add("C++");
  
        // Convert ArrayList to JSON string
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String jsonString
                = objectMapper.writeValueAsString(courses);
  
            // Print JSON string
            System.out.println(jsonString);
        }
        catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
}

Output:

JSON String output in console

Explanation of the above Program:

  • It creates an ArrayList of type String and adds 3 course names to it.
  • It uses the ObjectMapper from Jackson to convert the ArrayList to a JSON string.
  • It catches any exceptions that may occur during the JSON conversion.
  • It prints out the JSON string representation of the ArrayList to the console.



Reffered: https://www.geeksforgeeks.org


Java

Related
How to Convert a HashSet to JSON in Java? How to Convert a HashSet to JSON in Java?
Extract a Substring Between Two Characters in a String in Java Extract a Substring Between Two Characters in a String in Java
How to Convert an ArrayList Containing Integers to Primitive Int Array? How to Convert an ArrayList Containing Integers to Primitive Int Array?
Array with Constant Time Insertions and Deletions in Java Array with Constant Time Insertions and Deletions in Java
How to Create a TreeMap in Java and Add Key-Value Pairs in it? How to Create a TreeMap in Java and Add Key-Value Pairs in it?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
15