Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing Data in JSON file using Java

Tags:

java

json

arrays

I'm new to JSON. I've list of data which I can show in my console when I execute file. I want to store that data in JSON file using JSON array and JSON Object. I can store data in JSON file but I would have to put each data manually in the array, its very time consuming and I do have a lot of data to enter. So, I am looking for a way to read the data instead of printing it in console, I want to store it in JSON file.

This is how it should look in array.


   {
           "employee": 
           {
              "id": "100",

              "name": "ABC",

              "address": "New York"
           }
    }

Any suggestion on how to do it?

Update

The data which I'm printing on console is already imported from an excel file.

like image 882
AngDev Avatar asked Apr 25 '26 23:04

AngDev


1 Answers

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);

        }
}

}
like image 200
Leung King Tim Avatar answered Apr 27 '26 13:04

Leung King Tim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!