我需要连接BasicDBList中的所有BasicDBObject。每次循环运行时,我的BasicDBObject只包含ONE json元素,并退出我的BasicDBList不包含任何内容。
为什么会发生这种情况?将dbLinha.clear()
放在避免重复但每次循环运行时评估代码BasicDBList包含重复!为什么我不能连接JSON?
public BasicDBList readMetadados(Planilha planilha) {
List<String> cabecalho = new ArrayList<>();
int linhaReferencia = 0;
BasicDBObject dbLinha = new BasicDBObject();
BasicDBList listLinha = new BasicDBList();
try {
InputStream planilhaFile = new FileInputStream(FileUtils.getFile(UPLOAD_PATH, planilha.getPath()));
Sheet linhaInicial = new XSSFWorkbook(planilhaFile).getSheetAt(0);
Iterator<Row> rowIterator = linhaInicial.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
try {
if (cell.getCellType() != 3) {
if (cell.getCellType() == 1) {
if ("Veículo".equals(cell.getStringCellValue())) {
linhaReferencia = cell.getRow().getRowNum();
cabecalho.add(cell.getStringCellValue());
while (cellIterator.hasNext()) {
cabecalho.add(cellIterator.next().getStringCellValue());
}
break;
}
}
if (linhaReferencia != 0) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_FORMULA:
dbLinha.append(cabecalho.get(cell.getColumnIndex()), cell.getCellFormula());
break;
case Cell.CELL_TYPE_BOOLEAN:
dbLinha.append(cabecalho.get(cell.getColumnIndex()), cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
dbLinha.append(cabecalho.get(cell.getColumnIndex()), cell.getNumericCellValue());
break;
default:
dbLinha.append(cabecalho.get(cell.getColumnIndex()), cell.getStringCellValue());
}
}
}
} catch (IllegalStateException e) {
Log.info(this, "Erro ao obter valor da linha [{}] e coluna [{}]", cell.getRow().getRowNum(), cell.getColumnIndex());
}
}
if (!dbLinha.isEmpty()) {
for(int i = 0; i < cabecalho.size(); i++){
if(!dbLinha.containsKey(cabecalho.get(i))){
dbLinha.append(cabecalho.get(i), " ");
}
}
listLinha.add(dbLinha);
dbLinha.clear();
}
}
} catch (FileNotFoundException e) {
Log.error(this, "Erro ao processar planilha: Planilha não encontrada.", e);
} catch (IOException e) {
Log.error(this, "Erro ao processar planilha.", e);
}
System.out.println(listLinha.toString());
return listLinha;
}
输出
[ { } , { } , { } , { } , { } , { }]
BasicDBList的内容你第一次运行时是正确的,第二次开始复制和添加的anteriormentes取代。
BasicDBList的第一次运行循环中的值( “如果(!dbLinha.isEmpty())”)
第二次
如果你可以将这个提取成一个* short *但是* complete *程序来证明这个问题,那真的很有帮助... –
为了清晰起见编辑了这个问题。主要问题是连接“listLinha”中的所有JSON –
您仍然没有提供一个简短但完整的程序来演示问题,尽管... –