猿问

通过 json 模式将 map 转换为 json 字符串

有没有办法通过 json 模式将地图转换为 json 字符串?我需要通过 json 模式来做到这一点,因为我不知道地图中的对象是字符串还是数字。例如,我的 csv 看起来像这样:


name, year

1   , 1

我需要将它转换为 json 字符串"{'name': '1', 'year': 1}",我只能通过 json 模式知道 1 是字符串(在名称的情况下)还是数字(在年份的情况下)。


狐的传说
浏览 220回答 4
4回答

慕慕森

Jackson 有一个解析 CSV文档的模块。假设您的项目中已经有 Jackson 依赖项,您需要根据需要添加以下内容:<dependency>&nbsp; &nbsp; <groupId>com.fasterxml.jackson.dataformat</groupId>&nbsp; &nbsp; <artifactId>jackson-dataformat-csv</artifactId>&nbsp; &nbsp; <version>${jackson.version}</version></dependency>然后创建一个类来定义您的架构并保存值(注意@JsonPropertyOrder注释,因为它定义了 CSV 列的顺序):@JsonPropertyOrder({"name", "year"})public class Person {&nbsp; &nbsp; private String name;&nbsp; &nbsp; private Integer year;&nbsp; &nbsp; // Getters and setters&nbsp; &nbsp;}将 CSV 文档解析为列表,最后将列表写入 JSON 字符串:CsvMapper mapper = new CsvMapper();CsvSchema schema = mapper.schemaFor(Person.class).withHeader();MappingIterator<Object> iterator = mapper.readerFor(Person.class)&nbsp; &nbsp; &nbsp; &nbsp; .with(schema).readValues(new FileInputStream("/path/to/the/csv/file"));String json = new ObjectMapper().writeValueAsString(iterator.readAll());

慕后森

您可以使用 Jackson 尝试这样的事情:您唯一需要自己实现的是将 CsvSchema.json 转换为包含列名和列类型的 Map 的第二种方法。&nbsp; &nbsp; &nbsp;public String generateJsonFromCSV() throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; File csvFile = new File("path/to/csv/mycsv.csv");&nbsp; &nbsp; &nbsp; &nbsp; File schemaJson = new File("path/to/json/schema.json");&nbsp; &nbsp; &nbsp; &nbsp; Map<String, CsvSchema.ColumnType> map = getSchemaMapFromJson(schemaJson);&nbsp; &nbsp; &nbsp; &nbsp; CsvSchema.Builder schemaBuilder = new CsvSchema.Builder();&nbsp; &nbsp; &nbsp; &nbsp; map.forEach(schemaBuilder::addColumn);&nbsp; &nbsp; &nbsp; &nbsp; CsvSchema schema = schemaBuilder.build();&nbsp; &nbsp; &nbsp; &nbsp; CsvMapper csvMapper = new CsvMapper();&nbsp; &nbsp; &nbsp; &nbsp; MappingIterator<Map<?, ?>> mappingIterator = csvMapper.readerFor(Map.class).with(schema).readValues(csvFile);&nbsp; &nbsp; &nbsp; &nbsp; String json = new ObjectMapper().writeValueAsString(mappingIterator.readAll());&nbsp; &nbsp; &nbsp; &nbsp; return json;&nbsp; &nbsp; }&nbsp; &nbsp; //Convert your JsonSchema to Map<String,CsvSchema.ColumnType>&nbsp; &nbsp; private Map<String, CsvSchema.ColumnType> getSchemaMapFromJson(File file) {&nbsp; &nbsp; &nbsp; &nbsp; return new HashMap<>();&nbsp; &nbsp; }依赖:<dependency>&nbsp; &nbsp; <groupId>com.fasterxml.jackson.dataformat</groupId>&nbsp; &nbsp; <artifactId>jackson-dataformat-csv</artifactId>&nbsp; &nbsp; <version>${jackson.version}</version></dependency>

慕侠2389804

您可以使用 org.json 库:Map<String, Object> map = new HashMap<>();map.put("name", "1");map.put("year", 1);org.json.JSONObject obj = new org.json.JSONObject(map);System.out.println(obj.toString());输出:{"year":1,"name":"1"}

翻阅古今

您可以使用 GSON。它非常易于使用且功能强大。现在我能够解决这个库的所有问题。(https://github.com/google/gson)// your mapMap<String, Object> map = new HashMap<>();map.put("name", "1");map.put("year", 1);// create Gson instanceGson gson = new GsonBuilder().create();// convert to JSONString jsonString = gson.toJson(map);// convert back to mapMap map = gson.fromJson(jsonString, Map.class);
随时随地看视频慕课网APP

相关分类

Java
我要回答