JSON转化为CSV

  public static void main(String[] args) throws IOException {
        List<String> fs= FileUtil.readLocalFile("G://exportx (2).json",2487);
        File file = new File(   "G://" + "aa.txt" );
        BufferedWriter bw = null;
        bw = new BufferedWriter( new FileWriter(file) );
        for(String str:fs){
            Gson gson=new Gson();
            System.out.println(str);
            Map map= gson.fromJson(str, Map.class);
            String id=(String)map.get("id");
            Double c1=(Double)map.get("1");
            Double c2=(Double)map.get("2");
            Double c3=(Double)map.get("3");
            Double c4=(Double)map.get("4");
            String time=(String)map.get("time");
            bw.write(c1+","+c2+","+c3+","+c4+","+id+","+time);
            bw.newLine();
        }
        bw.close();
    }
  public static List<String> readLocalFile(String path, int lines){
        List<String> content = new ArrayList<>();
        try {
            FileInputStream inputStream = new FileInputStream(path);
            InputStreamReader streamReader = new InputStreamReader(inputStream, "UTF-8");
            BufferedReader bufferedReader = new BufferedReader(streamReader);
            String line = "";
            int count = 0;
            while ((line = bufferedReader.readLine()) != null && count < lines){
                content.add(line);
                count ++;
            }
        }catch (Exception e){
            logger.error(e.getMessage(), e);
        }
        return content;
    }

数据:
{“id”:“40000301”,“time”:“2021-05-17 06:59:59”,“2”:2147.0,“1”:31.8,“3”:58.6,“4”:29.2}
{“id”:“40000301”,“time”:“2021-05-17 06:59:59”,“2”:2147.0,“1”:31.8,“3”:58.6,“4”:49.2}
{“id”:“40000301”,“time”:“2021-05-17 07:00:02”,“2”:2147.0,“1”:31.8,“3”:18.6,“4”:11.2}
{“id”:“40000301”,“time”:“2021-05-17 07:00:03”,“2”:2147.0,“1”:31.8,“3”:18.6,“4”:11.2}
{“id”:“40000301”,“time”:“2021-05-17 07:00:03”,“2”:2147.0,“1”:31.8,“3”:18.6,“4”:6211.2}

你可能感兴趣的:(java)