如何把csv文件轉換為json格式


public class CSVToJSon {
 
    private List<String> stringToList(String s, String sep) {
        if (s == null) {
            return null;
        }
 
        String[] parts = s.split(sep);
        return Arrays.asList(parts);
    }
 
    private String stringToJson(List<String> header, List<String> lineData) throws Exception {
 
        if (header == null || lineData == null) {
            throw new Exception("輸入不能為null。");
        } else if (header.size() != lineData.size()) {
            throw new Exception("表頭個數和數據列個數不等。");
        }
 
        StringBuilder sBuilder = new StringBuilder();
        sBuilder.append("{ ");
        for (int i = 0; i < header.size(); i++) {
            String mString = String.format("\"%s\":\"%s\"", header.get(i), lineData.get(i));
            sBuilder.append(mString);
 
            if (i != header.size() - 1) {
                sBuilder.append(", ");
            }
        }
        sBuilder.append(" }");
 
        return sBuilder.toString();
    }
 
    public void ConvertToJson(InputStream filePath, OutputStream outPutPath) throws Exception {
       InputStreamReader isr=new InputStreamReader(filePath,"utf-8");

  BufferedReader reader=new BufferedReader(isr);

  OutputStreamWriter osw=new OutputStreamWriter(outPutPath,"utf-8");

  BufferedWriter reader=new BufferedWriter(osw);


        try {
            String sep = ",";
            String headerStr = reader.readLine();
 
            if (headerStr.trim().isEmpty()) {
                System.out.println("表格頭不能為空");
                return;
            }
            
            List<String> header = stringToList(headerStr, sep);
            String line;
            int lineCnt = 1;
            while ((line = reader.readLine()) != null) {
 
                if (line.trim().isEmpty()) {
                    System.out.println("第" + lineCnt + "行為空,已跳過");
                    continue;
                }
                
                List<String> lineData = stringToList(line, sep);
 
                if (lineData.size() != header.size()) {
                    String mString = String.format("第%d行數據列和表頭列個數不一致\r\n%s", lineCnt, line);
                    System.err.println(mString);
                    break;
                }
 
                String jsonStr = stringToJson(header, lineData);
                writer.write(jsonStr);
                writer.write("\r\n");
 
                lineCnt++;
            }
        } finally {
            if (reader != null) {
                reader.close();
            }
            if (writer != null) {
                writer.close();
            }
        }
    }
 
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        InputStream filePath=new FileInputStream("路徑");
   OutputStream outPutPath=new OutputStream("路徑");
        CSVToJSon csvToJSon = new CSVToJSon();
        csvToJSon.ConvertToJson(filePath, outPutPath);
        System.out.println("處理完成。");
    }
 
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM