Javatpoint Logo
Javatpoint Logo

How to Convert JSON Array to ArrayList in Java

In Java, JSON plays an important role in storing data. ArrayList is a special type of Array whose size is dynamic. It is also used to store or remove data anytime. ArrayList uses all the method of List and maintain the insertion order because it implements the List Interface. In some cases, we get the data in the JSON format and to work on it, we need to convert it into an Array-like structure. ArrayList is the best way to access data, and beginners are mostly asked how to convert JSON Array to ArrayList.

The server returns the response in JSON object or JSON Array, and to work on it efficiently, we need to convert it into an Array-like structure, e.g., ArrayList. The process of converting JSON Array into ArrayList is quite simple. We just need to iterate the JSON Array and add each element of the Array to the ArrayList.

In order to convert the JSON Array to ArrayList

  1. We will use the Maven because it is as easy as adding a dependency.
  2. We will add the following dependency to work with JSONArray and JSONObject.
  1. We will convert the JSON string into a JSON object.
  2. We will get the JSON array from the JSON object.
  3. We will create an empty array list of type Object.
  4. After that, we will iterate the JSON Array to push each element of it into ArrayList.
How to Convert JSON Array to ArrayList in Java

Let's see an example to understand how to convert the JSON Array into ArrayList.

We have the following json string from which we read the JSON data.

JSONArrayToArrayList.java

Output:

JSON Object
{"languages":[{"name":"Java","description":"Java is a class-based high-level programming language that follows the OOPs concepts."},{"name":"Javascript","description":"JavaScript is also a high-level, often just-in-time compiled, and multi-paradigm programming language."},{"name":"Python","description":"Python is another high-level, interpreted and general-purpose programming language."}]}
JSON Array
[{"name":"Java","description":"Java is a class-based high-level programming language that follows the OOPs concepts."},{"name":"Javascript","description":"JavaScript is also a high-level, often just-in-time compiled, and multi-paradigm programming language."},{"name":"Python","description":"Python is another high-level, interpreted and general-purpose programming language."}]
Each element of ArrayList
{"name":"Java","description":"Java is a class-based high-level programming language that follows the OOPs concepts."}
{"name":"Javascript","description":"JavaScript is also a high-level, often just-in-time compiled, and multi-paradigm programming language."}
{"name":"Python","description":"Python is another high-level, interpreted and general-purpose programming language."}





Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA