Java 为什么可以';我不能连接JSON吗?

Java 为什么可以';我不能连接JSON吗?,java,json,apache-poi,Java,Json,Apache Poi,我需要在BasicDBList中连接所有BasicDBObject。每次循环运行时,我的BasicDBObject只包含一个json元素,退出时我的BasicDBList不包含任何内容

我需要在BasicDBList中连接所有BasicDBObject。每次循环运行时,我的BasicDBObject只包含一个json元素,退出时我的BasicDBList不包含任何内容<为什么会发生这种情况?放置
dbLinha.clear()
以避免重复,但每次循环运行时都要对代码进行评估BasicDBList包含重复项

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的内容是正确的,第二次开始复制并替换为添加的AnterioRMENTE

第一次运行循环时BasicDBList的值(“if(!dbLinha.isEmpty()))

第二次
您正试图通过使用
清除
并反复使用同一对象(
dbLinha
)来保存对象。那不行

将对象添加到列表时,将添加对该对象的引用,而不是该对象的副本。基本上,第一次添加的是对
dbLinha
对象的引用,现在列表中的第一项指向
dbLinha
设置为的同一个对象

然后调用
dbLinha.clear()

这意味着存储在列表中的引用(相同)现在将显示一个空对象。然后将另一行读入同一对象,将另一个引用添加到列表中,然后再次清除它

您的列表将充满对您正在使用的单个对象的引用。下面是正在发生的事情的演示:


如果要保留对象,必须使用
new
,而不是
clear
。您必须创建一个新对象来存储下一位数据,因为添加到列表不会创建副本,而只是创建一个引用。因此,您基本上必须让添加的引用指向旧对象,然后从一个新对象开始。

如果您可以将其提取为一个简短但完整的程序来演示问题,这将非常有帮助……为了清晰起见,请编辑问题。主要的问题是将所有JSON连接到“listLinha”中,您还没有提供一个简短但完整的程序来演示这个问题……哇,令人惊讶的响应。我真的以为名单上有一份。
[ { } , { } , { } , { } , { } , { }]