BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Convert JSON Array to ArrayList in Java

Last Updated: June 13, 2024 by Chaitanya Singh | Filed Under: java

In this guide, you will learn how to convert JSON array to ArrayList in Java. To parse the given JSON data, you can use org.json package of Java. Let’s start step by step guide:

1. Add the JSON Library: In order to parse the JSON data, make sure that you have org.json library in your project. In case you are using Maven, add the following dependency to your pom.xml:

<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20230227</version>
</dependency>

    2. Parse the JSON Array and Convert to ArrayList: Let’s write the code to convert the JSON array to ArrayList.

    import org.json.JSONArray;
    import org.json.JSONObject;
    import java.util.ArrayList;

    public class JsonToArrayList {
    public static void main(String[] args) {
    // This is our JSON array contains name and age
    String jsonArrayString = "[{\"name\":\"Chaitanya\", \"age\":37}, {\"name\":\"Rahul\", \"age\":34}]";

    // Convert JSON array to JSONArray
    JSONArray jsonArray = new JSONArray(jsonArrayString);

    // Convert JSONArray to ArrayList
    ArrayList<JSONObject> arrayList = new ArrayList<>();
    for (int i = 0; i < jsonArray.length(); i++) {
    arrayList.add(jsonArray.getJSONObject(i));
    }

    // Print the ArrayList
    for (JSONObject obj : arrayList) {
    System.out.println(obj.toString());
    }
    }
    }

    Explanation:

    • JSONArray and JSONObject belong to org.json package. You must include this package in your code.
    • The jsonArrayString is the JSON array in string form.
    • The object of JSONArray is created by passing jsonArrayString in the constructor of JSONArray class.
    • The ArrayList that we have created is of JSONObject type, each element of this ArrayList represents the element of JSONArray.
    • Finally, the ArrayList is printed which contains the same elements that are present in JSONArray.

    Example with Custom Objects

    If you want the ArrayList of custom objects as output then you need to create a custom class to map the JSON object. In previous example, the JSON array contain the person name and age data. Let’s create a custom class Person with name and age as instance variables to map JSON object.

    import org.json.JSONArray;
    import org.json.JSONObject;
    import java.util.ArrayList;

    class Person {
    private String name;
    private int age;

    // Constructor
    public Person(String name, int age) {
    this.name = name;
    this.age = age;
    }

    @Override
    public String toString() {
    return "Person{name='" + name + "', age=" + age + "}";
    }
    }

    public class JsonToArrayList {
    public static void main(String[] args) {
    // This is our JSON array contains name and age
    String jsonArrayString = "[{\"name\":\"Chaitanya\", \"age\":37}, {\"name\":\"Rahul\", \"age\":34}]";

    // Convert JSON array to JSONArray
    JSONArray jsonArray = new JSONArray(jsonArrayString);

    // Convert JSONArray to ArrayList of Person type
    ArrayList<Person> arrayList = new ArrayList<>();
    for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject jsonObject = jsonArray.getJSONObject(i);
    String name = jsonObject.getString("name");
    int age = jsonObject.getInt("age");
    arrayList.add(new Person(name, age));
    }

    // Print the ArrayList
    for (Person person : arrayList) {
    System.out.println(person);
    }
    }
    }

    Top Related Articles:

    1. How to Find length of an Integer in Java
    2. Instance Variables in Java – Definition and Usage
    3. Java Collections Interview Questions and Answers
    4. Thread life cycle in java and thread scheduling
    5. Multilevel inheritance in java with example

    Tags: Java-ArrayList

    About the Author

    I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

    – Chaitanya

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Java Tutorial

    Java Introduction

    • Java Index
    • Java Introduction
    • History of Java
    • Features of Java
    • C++ vs Java
    • JDK vs JRE vs JVM
    • JVM - Java Virtual Machine
    • First Java Program
    • Variables
    • Data Types
    • Operators

    Java Flow Control

    • Java If-else
    • Java Switch-Case
    • Java For loop
    • Java while loop
    • Java do-while loop
    • Continue statement
    • break statement

    Java Arrays

    • Java Arrays

    OOPs Concepts

    • OOPs Concepts
    • Constructor
    • Java String
    • Static keyword
    • Inheritance
    • Types of inheritance
    • Aggregation
    • Association
    • Super Keyword
    • Method overloading
    • Method overriding
    • Overloading vs Overriding
    • Polymorphism
    • Types of polymorphism
    • Static and dynamic binding
    • Abstract class and methods
    • Interface
    • Abstract class vs interface
    • Encapsulation
    • Packages
    • Access modifiers
    • Garbage Collection
    • Inner classes
    • Static import
    • Static constructor

    Java Exception Handling

    • Exception handling
    • Java try-catch
    • Java throw
    • Java throws
    • Checked and Unchecked Exceptions
    • Jav try catch finally
    • Exception Examples
    • Exception Propagation

    Collections Framework

    • Collections in Java
    • Java ArrayList
    • Java LinkedList
    • Java Vector
    • Java HashSet
    • Java LinkedHashSet
    • Java TreeSet
    • Java HashMap
    • Java TreeMap
    • Java LinkedHashMap
    • Java Queue
    • Java PriorityQueue
    • Java Deque
    • Comparable interface
    • Comparator interface
    • Collections Interview Questions

    MORE ...

    • Java Scanner Class
    • Java 8 Features
    • Java 9 Features
    • Java Conversion
    • Java Date
    • Java Multithreading
    • Java I/O
    • Java Serialization
    • Java Regex
    • Java AWT
    • Java Swing
    • Java Enum
    • Java Annotations
    • Java main method
    • Java Interview Q

    Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap