create a class Employee
public class Employee {
private String employee;
public String id, name, address;
public Employee(String id, String name, String address) {
this.employee = "employee";
this.id = id;
this.name = name;
this.address = address;
}
Employee(String string) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public String getEmployee() {
return employee;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public void setEmployee(String employee) {
this.employee = employee;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAddress(String address) {
this.address = address;
}
}
main
import org.json.JSONObject;
import java.util.ArrayList;
import java.io.*;
public class JavaApplication1 {
public static void main(String[] args) {
ArrayList<Employee> array = new ArrayList<Employee>();
for(int i = 0 ; i < 100; i++){
array.add(new Employee(i+"", i+"", i+""));
}
JSONArray jsonArray = new JSONArray();
for (int i = 0;i < array.size() ; i++) {
JSONObject obj = new JSONObject();
JSONObject objItem = new JSONObject();
objItem.put("id", array.get(i).getId());
objItem.put("name", array.get(i).getName());
objItem.put("address", array.get(i).getAddress());
obj.put("employee", objItem);
jsonArray.put(obj);
}
try (FileWriter file = new FileWriter("your path")) {
file.write(jsonArray.toString());
System.out.println("Successfully Copied JSON Object to File...");
System.out.println("\nJSON Object: " + jsonArray);
} catch(Exception e){
System.out.println(e);
}
}
}