Converting an CSV file to a JSON object in Java

前端 未结 9 1703
太阳男子
太阳男子 2020-12-05 21:47

Is there an open source java library to convert a CSV (or XLS) file to a JSON object?

I tried using json.cdl, but somehow it does not seem to work for large CSV stri

9条回答
  •  被撕碎了的回忆
    2020-12-05 22:30

    I have used excel file in this code.you can use csv. i have wrote this class for particular Excel/csv format which is known to me.

    import java.io.File;
    
    public class ReadExcel {
    
        private String inputFile;
    
        public void setInputFile(String inputFile) {
            this.inputFile = inputFile;
        }
    
        public void read() throws IOException {
            File inputWorkbook = new File(inputFile);
            Workbook w;
            try {
                w = Workbook.getWorkbook(inputWorkbook);
                // Get the first sheet
                Sheet sheet = w.getSheet(0);
                // Loop over first 10 column and lines
                int columns = sheet.getColumns();
                int rows = sheet.getRows();
                ContactList clist = new ContactList();
                ArrayList contacts = new ArrayList();
                for (int j = 1; j < rows; j++) {
                    Contact contact = new Contact();
                    for (int i = 0; i < columns; i++) {
                        Cell cell = sheet.getCell(i, j);
    
                        switch (i) {
                        case 0:
                            if (!cell.getContents().equalsIgnoreCase("")) {
                                contact.setSrNo(Integer.parseInt(cell.getContents()));
                            } else {
                                contact.setSrNo(j);
                            }
    
                            break;
                        case 1:
                            contact.setName(cell.getContents());
                            break;
                        case 2:
                            contact.setAddress(cell.getContents());
                            break;
                        case 3:
                            contact.setCity(cell.getContents());
                            break;
                        case 4:
                            contact.setContactNo(cell.getContents());
                            break;
                        case 5:
                            contact.setCategory(cell.getContents());
                            break;
                        }
    
                    }
                    contacts.add(contact);
    
                }
                System.out.println("done");
                clist.setContactList(contacts);
                JSONObject jsonlist = new JSONObject(clist);
                File f = new File("/home/vishal/Downloads/database.txt");
                FileOutputStream fos = new FileOutputStream(f, true);
                PrintStream ps = new PrintStream(fos);
    
                ps.append(jsonlist.toString());
    
            } catch (BiffException e) {
                e.printStackTrace();
                System.out.println("error");
            }
        }
    
        public static void main(String[] args) throws IOException {
            ReadExcel test = new ReadExcel();
            test.setInputFile("/home/vishal/Downloads/database.xls");
            test.read();
        }
    }
    

    i have used jxl.jar for excel reading

提交回复
热议问题