我需要連接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 –
您仍然沒有提供一個簡短但完整的程序來演示問題,儘管... –