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
andJSONObject
belong toorg.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 passingjsonArrayString
in the constructor ofJSONArray
class. - The ArrayList that we have created is of
JSONObject
type, each element of this ArrayList represents the element ofJSONArray
. - Finally, the
ArrayList
is printed which contains the same elements that are present inJSONArray
.
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);
}
}
}
Leave a Reply