源代码:
import java.io.File;
import java.util.List;
import java.util.Map;
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;
public class ConvertCSVtoJson {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
File input = new File("C:\\Users\\prshanka\\Documents\\Report.csv");
CsvSchema csvSchema = CsvSchema.builder().setUseHeader(true).build();
CsvMapper csvMapper = new CsvMapper();
// Read data from CSV file
List<Object> readAll = (csvMapper).readerFor(Map.class).with(csvSchema).readValues(input).readAll();
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
// Write JSON formated data to output.json file
for (Object row : readAll) {
Map<String, String> map = (Map<String, String>) row;
String fileName = map.get("fileName");
File output = new File("C://Users//prshanka//Documents//Target" + fileName + ".txt");
mapper.writerWithDefaultPrettyPrinter().writeValue(output, row);
}
}
}
我收到错误的行:
List<Object> readAll = (csvMapper).readerFor(Map.class).with(csvSchema).readValues(input).readAll();
错误日志:
Error- The method readerFor(Class<Map>) is undefined for the type CsvMapper.
我已经在构建路径上添加了所有依赖项,但它仍然无法正常工作。