简体   繁体   English

使用 Java 将数据存储在 JSON 文件中

[英]Storing Data in JSON file using Java

I'm new to JSON.我是 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.我想使用 JSON 数组和 JSON Z497031794414AAC5524B53 将数据存储在 JSON 文件中。 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.我可以将数据存储在 JSON 文件中,但我必须手动将每个数据放入数组中,这非常耗时,而且我确实有很多数据要输入。 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.因此,我正在寻找一种读取数据而不是在控制台中打印数据的方法,我想将其存储在 JSON 文件中。

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.我在控制台上打印的数据已经从 excel 文件中导入。

Or use Jackson JSON library.或者使用 Jackson JSON 库。 https://github.com/FasterXML/jackson . https://github.com/FasterXML/jackson Anything you need to do with JSON you can do it using Jackson or even GSON (google JSON library)您需要使用 JSON 做的任何事情,您都可以使用 Jackson 甚至 GSON (谷歌 Z0ECD11Z1C1D7A2874223D)来完成

create a class Employee创建一个 class 员工

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

        }
}

}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM