一点点的咸菜在这里。我正在阅读Zip文件中的JSON,并且想要使用JSON的内容填充Vaadin中的表格。爪哇 - 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异常,堆栈跟踪但不包含任何我的课的,据我可以看到
作为第一步,我将设置一个POJO类,并为每个单元格连成员。使用包含JSON数据的POJO成功填充Java容器后,可以使用容器(例如BeanItemContainer)将其绑定到表。 'table.setContainerDataSource(new BeanItemContainer <>(POJO.class,yourListOfPojos))' –
Nevermind。过了太久。在我尝试填充表格之前,我的表格未正确初始化,导致出现可预期的空白点。其余的代码工作。 – Eskir