这是我的函数来读取内容并填充表格,这是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,但是就我所见,堆栈跟踪不包含任何类。
答案 0 :(得分:2)
以下是错误的:
String构造函数需要读取大小(n
)。
while ((n = zin.read(buffer, 0, 1024)) > -1) {
String JSON = new String(buffer, 0, n, StandardCharsets.UTF_8);
然后你在循环中执行最多1024个JSON,而不是一个JSON全部
UTF-8的字节不能在某个位置分开,比如位置1024,并且期望在结束时和块开始后有一个有效的完整多字节序列。
< / LI>此外还有readFully
和closeEntry
缺失。
简而言之:
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也会自动关闭。