【问题标题】:Convert CSV to JSON array in Java Springboot在 Java Spring Boot 中将 CSV 转换为 JSON 数组
【发布时间】:2020-05-14 19:49:00
【问题描述】:

您好,我正在尝试使用名为 csvReader 的依赖项将 CSV 文件转换为 JSON 数组,但是当我运行代码时,它会错误地打印出 JSON 响应,我知道为什么有人能够指出我的正确之处方向。

    @GetMapping("/convert")
    public List<List<String>> convertCSV() throws FileNotFoundException {
        List<List<String>> records = new ArrayList<List<String>>();
    try (CSVReader csvReader = new CSVReader(new FileReader("C:/Download/cities.csv"));) {
        String[] values = null;

    while ((values = csvReader.readNext()) != null) {
        records.add(Arrays.asList(values));
    }

} catch (IOException e) {
            e.printStackTrace();
        }
        return values;
    }

【问题讨论】:

  • 输出有什么问题?输出由字符串数组组成,我希望查看代码,因为convertCSV 返回List&lt;List&lt;String&gt;&gt;
  • 你能解释一下这个prints out the JSON response incorrectly 吗?什么不正确?
  • 我需要它来打印“latD”:“41”、“LatM”:“5”、“LatS”:“59”、“NS”:“N”等...我只需要将类型从列表更改为数组,为了获得更多上下文,我需要一个 Angular 7 前端才能遍历数组

标签: java spring-boot


【解决方案1】:

你的案子没什么大不了的。

您可以阅读 csv 并构建 json

读取第一行并确定列。其余行是值。

public class Foo{

    public static void main(String[] args) throws Exception{

        List<String> csvRows = null;
        try(var reader = Files.lines(Paths.get("dataFile.csv"))){
            csvRows = reader.collect(Collectors.toList());
        }catch(Exception e){
            e.printStackTrace();
        }

        if(csvRows != null){

            String json = csvToJson(csvRows);
            System.out.println(json);

        }

    }

    public static String csvToJson(List<String> csv){

        //remove empty lines
        //this will affect permanently the list. 
        //be careful if you want to use this list after executing this method
        csv.removeIf(e -> e.trim().isEmpty());

        //csv is empty or have declared only columns
        if(csv.size() <= 1){
            return "[]";
        }

        //get first line = columns names
        String[] columns = csv.get(0).split(",");

        //get all rows
        StringBuilder json = new StringBuilder("[\n");
        csv.subList(1, csv.size()) //substring without first row(columns)
            .stream()
            .map(e -> e.split(","))
            .filter(e -> e.length == columns.length) //values size should match with columns size
            .forEach(row -> {

                json.append("\t{\n");

                    for(int i = 0; i < columns.length; i++){
                        json.append("\t\t\"")
                            .append(columns[i])
                            .append("\" : \"")
                            .append(row[i])
                            .append("\",\n"); //comma-1
                    }

                    //replace comma-1 with \n
                    json.replace(json.lastIndexOf(","), json.length(), "\n");

                json.append("\t},"); //comma-2

            });

        //remove comma-2
        json.replace(json.lastIndexOf(","), json.length(), "");

        json.append("\n]");

        return json.toString();

    }

}

测试日期:

fname,lname,note
Shaun,Curtis,a
Kirby,Beil,b

-----------------------

[
    {
        "fname" : "Shaun",
        "lname" : "Curtis",
        "note" : "a"
    },  {
        "fname" : "Kirby",
        "lname" : "Beil",
        "note" : "b"
    }
]

此方法适用于csv 的任何结构。不需要映射列。

【讨论】:

  • 您能否告诉我如何为 java 1.8 编写此代码,我必须从 12 降级并接受 var isant?
  • var(在java10中引入)它就像一个快捷方式,表示它在声明时获得的值的类型。在这种情况下Stream&lt;String&gt;。有什么不兼容的吗?
  • 欢迎。提示:即使您使用dependency,请尝试为您的问题考虑自己的解决方案,即使它很糟糕并且因为您不使用它而浪费时间。到时候它会帮助你。
【解决方案2】:

那是因为您读取字符串中的数据并打印字符串列表。如果您想将 CSV 映射到 Object(JSON 对象),您需要将 CSV 作为 bean 对象读取,请在下面找到代码 sn-p 以打印为 JSON,将 toString 方法覆盖为 JSON 格式。

用户.java

public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull
    private String name;

    @NotNull
    private String surname;

    //Getter and Setters
}

CsvReaderUtil.java

public static List<User> readCsvFile() throws IOException {
            List<User> list = null;
            CSVReader reader = null;
            InputStream is = null;
            try {
                File initialFile = new File("C:\\Users\\ER\\Desktop\\test.csv");
                is = new FileInputStream(initialFile);
                reader = new CSVReader(new InputStreamReader(is), ',', '"', 1);
                ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy();
                strat.setType(User.class);
                String[] columns = new String[]{"id", "name", "surname"};
                strat.setColumnMapping(columns);
                CsvToBean csv = new CsvToBean();
                list = csv.parse(strat, reader);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                is.close();
                reader.close();
            }
            return list;
        }

现在将此用户列表打印为 JSON 对象。

【讨论】:

  • 我收到此错误:没有为类 com.poc.demo.models.Host 找到序列化程序,也没有发现用于创建 BeanSerializer 的属性
  • 能否请您也发布代码,以便我了解更多信息。
  • 问题是杰克逊需要公开的 getter 和 setter 在这里找到答案 -> stackoverflow.com/questions/8367312/… 代码并没有改变你发布的内容只是将文件重命名为来自用户的主机现在我只需要弄清楚为什么我会为空
猜你喜欢
  • 2019-04-19
  • 1970-01-01
  • 2022-01-06
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 2020-11-21
  • 1970-01-01
相关资源
最近更新 更多