繁体   English   中英

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

[英]Storing Data in JSON file using Java

我是 JSON 的新手。 我有执行文件时可以在控制台中显示的数据列表。 我想使用 JSON 数组和 JSON Z497031794414AAC5524B53 将数据存储在 JSON 文件中。 我可以将数据存储在 JSON 文件中,但我必须手动将每个数据放入数组中,这非常耗时,而且我确实有很多数据要输入。 因此,我正在寻找一种读取数据而不是在控制台中打印数据的方法,我想将其存储在 JSON 文件中。

这就是它在数组中的样子。


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

              "name": "ABC",

              "address": "New York"
           }
    }

关于如何做的任何建议?

更新

我在控制台上打印的数据已经从 excel 文件中导入。

或者使用 Jackson JSON 库。 https://github.com/FasterXML/jackson 您需要使用 JSON 做的任何事情,您都可以使用 Jackson 甚至 GSON (谷歌 Z0ECD11Z1C1D7A2874223D)来完成

创建一个 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;
    }


}

主要的

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

        }
}

}

这可能对你有帮助...

        //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