gpt4 book ai didi

Java - Vaadin - 表未填充

转载 作者:行者123 更新时间:2023-12-02 03:21:49 25 4
gpt4 key购买 nike

这里有一点泡菜。我正在从 Zip 文件读取 JSON,并且想用 JSON 的内容填充 Vaadin 中的表格。

这是我的函数,用于读取内容并填充表格,这是 Java。

private void getJsonContent() {
try {
FileInputStream fis = new FileInputStream(backupFile);
ZipInputStream zin = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
byte[] buffer = new byte[1024];
while((entry = zin.getNextEntry()) != null) {
if(entry.getName().equalsIgnoreCase("content.json")) {
int n;
while((n = zin.read(buffer, 0, 1024)) > -1){
String JSON = new String(buffer, Charset.forName("UTF-8"));
JSONObject obj = new JSONObject(JSON);

logger.info(JSON);

// Assign "global" Values to Variables
this.createdAt = obj.getString("created_at");
this.version = obj.getString("version");

// Fill table if applicable
for(int i = 0; i < obj.getJSONArray("content").length(); i++) {
JSONObject sub = obj.getJSONArray("content").getJSONObject(i);
logger.info(sub);

infoTable.addItem(new Object[] {
sub.get("imported_identities").toString(),
sub.get("project_versions").toString(),
sub.get("last_import").toString(),
sub.get("client").toString(),
sub.get("project").toString()
}, i +1);
}
}
}
}
zin.close();
fis.close();
} catch (FileNotFoundException e) {
// Can't happen here
} catch (IOException e) {
logger.info("Can't read File.");
} catch (JSONException jse) {
logger.info("JSON Content could not be read: " + jse.getMessage());
}
}

您会注意到我有一个函数调用 logger.info(sub) - 确保我得到的是另一个有效的 JSON 对象(我正在读取的文件包含嵌套的内容)

输出:

{"imported_identities":0,"project_versions":0,"last_import":null,"client":"Client1","project":"Project2"} 
{"imported_identities":0,"project_versions":0,"last_import":null,"client":"Client2","project":"Project1"}
{"imported_identities":0,"project_versions":1,"last_import":"2016-09-14T09:28:24.520Z","client":"Client1","project":"Project1"}

我确保所有值都是正确的(并且该表是使用 null 作为默认值构建的) - 这是表属性:

    infoTable.addContainerProperty(impIds, String.class, null);
infoTable.addContainerProperty(projVe, String.class, null);
infoTable.addContainerProperty(lstImp, String.class, null);
infoTable.addContainerProperty(client, String.class, null);
infoTable.addContainerProperty(projct, String.class, null);

infoTable.setColumnCollapsingAllowed(true);
infoTable.setColumnCollapsed(impIds, true);
infoTable.setColumnCollapsed(projVe, true);
infoTable.setColumnCollapsed(lstImp, true);

最后,表上调用了“refreshRowCache”。有人看到问题了吗?没有错误,什么也没有,表只是没有添加项目(调用后 infoTable.getItemIds().size() 的大小为 0。

编辑:

我尝试了以下方法来验证。

infoTable.addItem(i + 1);
infoTable.getItem(i + 1).getItemProperty(impIds).setValue(sub.get("imported_identities").toString());
infoTable.getItem(i + 1).getItemProperty(projVe).setValue(sub.get("project_versions").toString());

这导致了 NullPointerException,但是据我所知,堆栈跟踪不包含我的任何类。

最佳答案

下列说法错误的是:

  1. String 构造函数需要读取大小 (n)。

                while ((n = zin.read(buffer, 0, 1024)) > -1) {
    String JSON = new String(buffer, 0, n, StandardCharsets.UTF_8);
  2. 然后,您在循环中执行最多 1024 个 JSON,而不是全部处理一个 JSON

  3. UTF-8 的字节无法在某个点(例如位置 1024)进行分割,并且期望在末尾和 block 的开始处有一个有效的完整多字节序列。

  4. 此外,还缺少 readFullycloseEntry

简而言之:

private void getJsonContent() {
try (ZipInputStream zin = new ZipInputStream(new BufferedInputStream(
new FileInputStream(backupFile)))) {
ZipEntry entry;
while ((entry = zin.getNextEntry()) != null) {
if (entry.getName().equalsIgnoreCase("content.json")) {
long size = entry.getSize();
if (size > 100_000) {
throw new IllegalArgumentException("Data too large");
}
// We could use an InputStreamReader and read text piecewise.
// However JSON parsing also is easiest on an entire text.
byte[] buffer = new byte[(int)size];
int n = zin.readFully(buffer, 0, buffer.length);
String json = new String(buffer, StandardCharsets.UTF_8);
JSONObject obj = new JSONObject(json);
logger.info(json);

// Assign "global" Values to Variables
this.createdAt = obj.getString("created_at");
this.version = obj.getString("version");

// Fill table if applicable
for (int i = 0; i < obj.getJSONArray("content").length(); i++) {
JSONObject sub = obj.getJSONArray("content").getJSONObject(i);
logger.info(sub);

infoTable.addItem(new Object[] {
sub.get("imported_identities").toString(),
sub.get("project_versions").toString(),
sub.get("last_import").toString(),
sub.get("client").toString(),
sub.get("project").toString()
}, i + 1);
}
} // if
zin.closeEntry(); // Do not forget preparing for the next entry
}
} catch (IOException e) {
logger.info("Can't read File.");
} catch (JSONException jse) {
logger.info("JSON Content could not be read: " + jse.getMessage());
}
}

即使出现异常或返回,try-with-resources 也会自动关闭。

关于Java - Vaadin - 表未填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39492317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com