英文:
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
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("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<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.
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-csv</artifactId>
<version>2.11.1</version>
</dependency>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论