Java转换CSV到JSON时出现错误。forType和readAll方法。

huangapple go评论75阅读模式
英文:

Java Error while CSV to Json conversion. forType and readAll method

问题

import java.io.*;
import java.util.*;
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import com.fasterxml.jackson.databind.MappingIterator.*;

public class CsvtoJson {
    public static void main(String args[]) throws Exception {
        File input = new File("input.csv");
        try {
            CsvSchema csv = CsvSchema.emptySchema().withHeader();
            CsvMapper csvMapper = new CsvMapper();
            MappingIterator<Map<?, ?>> mappingIterator =  csvMapper.reader().forType(Map.class).with(csv).readValues(input);
            List<Map<?, ?>> list = mappingIterator.readAll();
            System.out.println(list);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

Compilation Error:-
The method forType(Class) is undefined for the type ObjectReader
The method readAll() is undefined for the type MappingIterator<Map>

Trying to convert a big CSV file into JSON with Jackson library but I am stuck with this error. All jars all correctly added in the build path.

英文:
import java.io.*;
import java.util.*;
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import com.fasterxml.jackson.databind.MappingIterator.*;


public class CsvtoJson {
	   public static void main(String args[]) throws Exception {
		      File input = new File(&quot;input.csv&quot;);
		      try {
		         CsvSchema csv = CsvSchema.emptySchema().withHeader();
		         CsvMapper csvMapper = new CsvMapper();
		         MappingIterator&lt;Map&lt;?, ?&gt;&gt; mappingIterator =  csvMapper.reader().forType(Map.class).with(csv).readValues(input);
		         List&lt;Map&lt;?, ?&gt;&gt; list = mappingIterator.readAll();
		        System.out.println(list);
		      } catch(Exception e) {
		         e.printStackTrace();
		      }
		   }
		}

Compilation Error:-
The method forType(Class<Map>) is undefined for the type ObjectReader
The method readAll() is undefined for the type MappingIterator<Map<?,?>>

Trying to convert a big CSV file into JSON with Jackson library but I am stuck with this error. All jars all correctly added in the build path.

答案1

得分: 1

这段代码在我的环境中运行良好(JDK13),我认为你可能有依赖性问题。请确保你拥有以下这些依赖的JAR包(版本并不是特别重要):

com.fasterxml.jackson.core:jackson-annotations:2.11.1
com.fasterxml.jackson.core:jackson-core:2.11.1
com.fasterxml.jackson.core:jackson-databind:2.11.1
com.fasterxml.jackson.dataformat:jackson-dataformat-csv:2.11.1

注意:使用IDE创建一个Maven项目会更加简单。在创建Maven项目时,将以下依赖添加到你的Maven POM文件中。

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-csv</artifactId>
    <version>2.11.1</version>
</dependency>
英文:

The code works fine for me (JDK13), I think you have dependency problem. Please make sure you have jars for these dependencies(versions are not much important):

com.fasterxml.jackson.core:jackson-annotations:2.11.1
com.fasterxml.jackson.core:jackson-core:2.11.1
com.fasterxml.jackson.core:jackson-databind:2.11.1
com.fasterxml.jackson.dataformat:jackson-dataformat-csv:2.11.1

Note: It is much easy to create a maven project with an IDE. When you create a maven project add this dependecy to your maven pom file.

&lt;dependency&gt;
    &lt;groupId&gt;com.fasterxml.jackson.dataformat&lt;/groupId&gt;
    &lt;artifactId&gt;jackson-dataformat-csv&lt;/artifactId&gt;
    &lt;version&gt;2.11.1&lt;/version&gt;
&lt;/dependency&gt;

huangapple
  • 本文由 发表于 2020年7月27日 03:49:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63104966.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定