![]() |
VOOZH | about |
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 of objects to a JSON array in Java.
Below are the steps and implementation of converting an ArrayList of objects to a JSON array with Jackson.
Open any one 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.
👁 Maven Project CreationBelow is the Project Structure that we have created.
👁 Project StructureNow, we will add Jackson dependency to XML file.
Now we will create a POJO class named Course.Java.
In the above code, a Java class called Course models the properties of a course like its id, name, fees, and duration. It has getter and setter methods to access and modify the private fields.
Here we are adding the logic of converting an ArrayList of objects to a JSON array of Java in the main class.
[ {"id":1 , "course_name":"Java" , "course_fees" : "25000" , "course_duration":"3 Months" } ,
{"id":2 , "course_name":"C++" , "course_fees" : "20000" , "course_duration":"3 Months" } ]