A Jackson is a Java JSON API that provides many different ways to process JSON. . We can convert CSV data to JSON data using the CsvMapper class, which is a special ObjectMapper with extended functionality that can convert POJOs into CsvSchema instances. We can use the reader() method to build an ObjectReader with default settings. In order to convert, we need to import the com.fasterxml.jackson.dataformat.csv package.
In the example below, convert CSV to JSON.
import java.io.*; import java.util.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.dataformat.csv.*; public class CsvToJsonTest { 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(); } } }
[{last name=Chandra, first name=Ravi, location=Bangalore}]
The above is the detailed content of Convert CSV to JSON using Jackson library in Java?. For more information, please follow other related articles on the PHP Chinese website!