Reputation: 35
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.
Upvotes: 3
Views: 13418
Reputation: 14698
Another way to store it in JSON file in java is to use Filelize.
Filelizer filelizer = new Filelizer("src/main/resources/");
filelizer.save("100", employee);
Upvotes: 0
Reputation: 76
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);
}
}
}
Upvotes: 2
Reputation: 31
This may be helpful to you...
//First User
JSONObject userDetails = new JSONObject();
userDetails.put("firstName", "Arun");
userDetails.put("lastName", "Kumar");
userDetails.put("website", "google.com");
JSONObject userObject = new JSONObject();
userObject.put("user", userDetails);
//Second User
JSONObject userDetails2 = new JSONObject();
userDetails2.put("firstName", "Brian");
userDetails2.put("lastName", "Schultz");
userDetails2.put("website", "example.com");
JSONObject userObject2 = new JSONObject();
userObject2.put("user", userDetails2);
//Add users to list
JSONArray userList = new JSONArray();
userList.add(userObject);
userList.add(userObject2);
//Write JSON file
try (FileWriter file = new FileWriter("users.json")) {
file.write(userList.toJSONString());
file.flush();
} catch (IOException e) {
e.printStackTrace();
}
Upvotes: 0