如何使用java将获取的数据转换为JSON

时间:2017-08-27 09:51:05

标签: java json excel

我已成功从excel表中获取数据,现在我想将获取的数据转换为JSON格式。具体格式。我该如何进一步转换部分?

Excel Sheet VIEW

JSON格式应为:

  

{机器:M / C1,时间:中午12:00,状态:工作,},{机器:   M / C2,时间:12:00 PM,状态:工作},} ......}

我为从Excel工作表中获取数据而编写的代码:

try {
        POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("F:\\software\\list.xls"));
        HSSFWorkbook wb = new HSSFWorkbook(fs);
        HSSFSheet sheet = wb.getSheetAt(0);
        HSSFRow row;
        HSSFCell cell;

        int rows; // No of rows
        rows = sheet.getPhysicalNumberOfRows();

        int cols = 0; // No of columns
        int tmp = 0;


        for(int i = 0; i < 10 || i < rows; i++) {
            row = sheet.getRow(i);
            if(row != null) {
                tmp = sheet.getRow(i).getPhysicalNumberOfCells();
                if(tmp > cols) cols = tmp;
            }
        }

        for(int r = 0; r < rows; r++) {
            row = sheet.getRow(r);
            if(row != null) {
                for(int c = 0; c < cols; c++) {
                    cell = row.getCell((short)c);
                    if(cell != null) {
                        // Your code here
                        System.out.println(cell);

                    }
                }
            }
        }
    } catch(Exception ioe) {
        ioe.printStackTrace();
    }

2 个答案:

答案 0 :(得分:1)

我建议你为JSON条目创建一个POJO类,例如:

public class MachineData {
    private String machine;
    private String time;
    private String status;

    public MachineData(String m, String t, String s) {
        this.machine = m;
        this.time = t;
        this.status = s;
    }

    //+ getters, setters
}

然后从您从Excel中提取的数据创建MachineData个对象。将它们放在列表中,然后您可以使用JacksonGSON将列表转换为JSON。

// create a list of MachineData objects:
List<MachineData> list = new LinkedList<>();

// then when you go through Excel rows:
    String machine = ... // parse from Excel
    String time = ... // parse from Excel
    String status = ... // parse from Excel

    // build a MachineData object
    MachineData md = new MachineData(machine, time, status);

    // and add it to the list:    
    list.add(md);

// after you finished with the Excel part
Gson gson = new Gson();
String json = gson.toJson(list); // here you have the JSON in a string

答案 1 :(得分:1)

您可以使用Gson(如果您不需要高性能)或Jackson(如果您关心性能)。

将gson作为依赖项添加到您的项目中。如果您使用Gradle,只需将此行添加到build.gradle dependecies部分:

compile group: 'com.google.code.gson', name: 'gson', version: '2.8.1'

或检查其他构建系统示例here

Gson的一切都很简单。为您的对象声明一个类:

public class MachineStatus {
    @SerializedName("Machine")
    String machine;
    @SerializedName("Time")
    String time;
    @SerializedName("Status")
    String status;
}

然后准备这样的对象列表

    ArrayList<MachineStatus> statuses = new ArrayList<>();
    for(int r = 0; r < rows; r++) {
        row = sheet.getRow(r);
        if(row != null) {
            for(int c = 0; c < cols; c++) {
                cell = row.getCell((short)c);
                if(cell != null) {
                    // Your code here
                    System.out.println(cell);
                    MachineStatus status = new MachineStatus();
                    status.machine = cell.getMachine(); // change this part to use correct method that will fetch data from cell
                    status.time = cell.getTime(); // same here
                    status.status = cell.getStatus(); // same here
                    statuses.add(status);
                }
            }
        }
    }

    Gson gson = new GsonBuilder()
            .setPrettyPrinting() // comment this out if you need to save bandwidth;
            .create();

    // Prints Json array string
    System.out.println(gson.toJson(statuses));

更多关于gson and collections of objects