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

如何使用java将获取的数据转换为JSON,java,json,excel,Java,Json,Excel,我已经成功地从excel工作表中获取了数据,现在我想将获取的数据转换为JSON格式。以特定的格式。如何做进一步的转换部分 JSON格式应为: {机器:M/C1,时间:下午12:00,状态:工作,},{机器: M/C2,时间:下午12:00,状态:工作},}……} 我为从excel工作表中获取数据而编写的代码: try { POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("F:\\software\\lis

我已经成功地从excel工作表中获取了数据,现在我想将获取的数据转换为JSON格式。以特定的格式。如何做进一步的转换部分

JSON格式应为:

{机器:M/C1,时间:下午12:00,状态:工作,},{机器: M/C2,时间:下午12:00,状态:工作},}……}

我为从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();
    }
试试看{
POIFSFISTEM fs=新的POIFSFISTEM(新文件输入流(“F:\\software\\list.xls”);
HSSF工作手册wb=新的HSSF工作手册(fs);
HSSFSheet sheet=wb.getSheetAt(0);
HSSFRow row;
HSSF细胞;
int rows;//行数
rows=sheet.getPhysicalNumberOfRows();
int cols=0;//列数
int-tmp=0;
for(int i=0;i<10 | | icols)cols=tmp;
}
}
对于(int r=0;r
我建议您为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
对象。将它们放入列表中,然后可以使用或将列表转换为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
//创建MachineData对象的列表:
列表=新建LinkedList();
//然后,在浏览Excel行时:
字符串机器=…//从Excel解析
字符串时间=…//从Excel解析
字符串状态=…//从Excel解析
//构建MachineData对象
MachineData md=新的MachineData(机器、时间、状态);
//并将其添加到列表中:
添加(md);
//完成Excel部分后
Gson Gson=新的Gson();
字符串json=gson.toJson(列表);//这里有一个字符串形式的JSON

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

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

compile group: 'com.google.code.gson', name: 'gson', version: '2.8.1'
或者查看其他构建系统示例

使用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));
arrayliststatus=new ArrayList();
对于(int r=0;r

更多信息。

您可以从几十个Java JSON解析器/格式化程序(Jackson、GSon、JSON Simple等)中选择一个,阅读文档,然后按照说明去做。我很确定几天前我看到了这个问题。编辑:这是一个类似的错误:(1)(2)我得到错误:在eclipse中SerializedName无法解析为类型。如何解决:(您是否已将gson依赖项添加到您的项目中?您使用哪种依赖项管理器,例如gradle/maven?maven,,com.google.code.gson gson 1.4尝试版本
2.8.1
-此版本
com.google.code.gson gson 2.8.1
很好,2.8.1正常,无错误:)我已经创建了另外3个getters,请在这里解释列表部分,它如何工作,一个简单的示例将起作用:)