如何使用java代码读取CSV和解析JSON
我使用以下代码读取CSV文件并将其数据解析为JSON如何使用java代码读取CSV和解析JSON,json,spring,gson,opencsv,csvreader,Json,Spring,Gson,Opencsv,Csvreader,我使用以下代码读取CSV文件并将其数据解析为JSON File inputFile = new File("in.csv"); File outputFile = new File("out.json"); CsvSchema csvSchema = CsvSchema.builder().setUseHeader(true).build(); CsvMapper csvMapper = new Cs
File inputFile = new File("in.csv");
File outputFile = new File("out.json");
CsvSchema csvSchema = CsvSchema.builder().setUseHeader(true).build();
CsvMapper csvMapper = new CsvMapper();
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
mapper.writerWithDefaultPrettyPrinter().writeValue(outputFile,
csvMapper.readerFor(Map.class).with(csvSchema).readValues(inputFile).readAll());
这是工作良好,给我的输出如下
[
{
"Nutrient" : "Calories",
"Amount" : " 289.00",
"Unit" : " kcal"
}, {
"Nutrient" : "Fat",
"Amount" : " 17.35",
"Unit" : " g"
}
]
但所需的输出是
{
{
"Nutrient" : "Calories",
"Amount" : " 289.00",
"Unit" : " kcal"
}, {
"Nutrient" : "Fat",
"Amount" : " 17.35",
"Unit" : " g"
}
}
实际上,我需要读取从CSV转换的JSONs文件。使用以下代码
String content = Files.readString(filePath);
JSONObject jsonObject1 = new JSONObject(content);
HashMap yourHashMap1 = new Gson().fromJson(jsonObject1.toString(), HashMap.class);
但是当我尝试去做的时候,它给了我这个错误
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
同时,json文件应该以{
开头,而不是[
,同样,它应该以}
结尾,而不是]
。
我的目标是消除这个错误。我认为您看到的结果是正确的,这就是JSON应该采用的方式。JSON是一种基于键值的格式
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
粘贴的输出意味着它是一个json对象数组
[
{
“营养素”:“卡路里”,
“金额”:“289.00”,
“单位”:“千卡”
}, {
“营养素”:“脂肪”,
“金额”:“17.35”,
“单位”:“g”
}
]
下面的JSON实际上毫无意义,因为对象没有键。即使您尝试使用Jackson解析此类JSON,它也会在线程“main”com.fasterxml.Jackson.core.JsonParseException中引发一个错误异常:意外字符(“{”(代码123)):应在字段名开头加双引号 在[Source:(String)“{。您可以尝试一下
{
{
“营养素”:“卡路里”,
“金额”:“289.00”,
“单位”:“千卡”
}, {
“营养素”:“脂肪”,
“金额”:“17.35”,
“单位”:“g”
}
}
另一种选择是将每个JSON对象视为具有以下名称的唯一节点,参见关键字SET1和SET2。
{
“set1”:{
“营养素”:“卡路里”,
“金额”:“289.00”,
“单位”:“千卡”
},
“set2”:{
“营养素”:“脂肪”,
“金额”:“17.35”,
“单位”:“g”
}
}
出于某种原因,如果您真的想用{}而不是[],那么只需执行一个字符串操作,并将第一个“[”替换为“{”,最后一个“]”替换为“}
编辑答案以匹配编辑的问题: 现在我们知道你的JSON是一个JSON对象数组,你必须把它作为JSONArray而不是JSONObject来读取,你也不能再把它读到一个hashmap,它必须是一个列表,列表中的每个元素都是一个JSONObject,其中包含你的数据
String content = Files.readString(filePath);
JSONArray jsonArray = new JSONArray(content);
List<LinkedTreeMap> yourList = new Gson().fromJson(jsonArray.toString(), ArrayList.class);
for(LinkedTreeMap l : yourList) {
System.out.println(l.get("Amount"));
System.out.println(l.get("Nutrient"));
System.out.println(l.get("Unit"));
}
String content=Files.readString(filePath);
JSONArray JSONArray=新JSONArray(内容);
List yourList=new Gson().fromJson(jsonArray.toString(),ArrayList.class);
for(LinkedTreeMap l:yourList){
系统输出打印项次(l.get(“金额”);
System.out.println(l.get(“营养”));
System.out.println(l.get(“单位”));
}
Sir,但我需要在HashMap中使用它。您更新的答案在线程“main”java.lang.IllegalArgumentException中给了我这个错误异常:传递的对象数必须是偶数,但在IndexRequest IndexRequest=new IndexRequest(index,TYPE)。source(yourList);
1.需要完整的错误堆栈跟踪(这是一个工作代码,因此不应该有任何异常,您能确认您的csv是否有固定的模式?固定的列数吗?)2.直接来说,您无法将此类JSON数据映射到任何hashmap,我们必须进行一些修改,请让我知道您希望如何将此数据表示为hashmap?什么应该是键,什么应该是值?只有这样我们才能得出结论