我尝试通过 REST API 从 JIRA 收集数据。
我可以获得带有 JSONObjects 的 jsonArray。这些 JSONObjects 可能包含属性 a;b;c;d。一些 JSONObjects 可能只包含例如 a;b;c 缺少属性 d。
我试图在代码中收集这些属性。由于某些 JSONObjects 缺少某些属性,我可能会收到这样的错误消息:
Exception in thread "main" org.json.JSONException: JSONObject["d"] not found.
我使用了 try/catch 方法(见下文),因此我通过忽略它来避免错误消息。
有没有更好的方法来解决这样的问题?
JsonNode jsonNode = response.getBody();
JSONArray jsonArray = jsonNode.getArray();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = (String) jsonObject.get("name");
String startDate = (String) jsonObject.get("startDate");
String releaseDate = (String) jsonObject.get("releaseDate");
Integer projectId = (Integer) jsonObject.get("projectId");
String description;
// getting the error for the attribute description
try {
description = (String) jsonObject.get("description");
} catch (Exception e) {
description = "";
}
慕森卡
相关分类