获取 jsonObject 的不存在属性的最佳方法
Optimal way to get a jsonObject's non Existent Attribute
我尝试通过 REST 从 JIRA 收集数据 API。
我可以用 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 = "";
}
创建一个 custom object
和 deserialize
的 json string
成这个对象的一个实例。我将在下面展示一个使用 gson
库
的解决方案
Maven 依赖
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
型号class
public class Model {
private boolean archived;
private String releaseDate;
private String name;
private String self;
private String userReleaseDate;
private long id;
private long projectId;
private boolean released;
//getters && setters ommitted
public Model(){
}
那你可以deserialize the JSON string into the Model class like this
String json = "{\"archived\":false,\"releaseDate\":\"2019-07-16\",\"name\":\"test 1.0\",\"self\":\"https://test/rest/api/latest/test/10000\",\"userReleaseDate\":\"16/Jul/19\",\"id\":\"10000\",\"projectId\":10000,\"released\":true}";
Gson gson = new GsonBuilder().create();
Model model = gson.fromJson(json, Model.class);
这样就不用每次都单独检查每个参数赋值给对应的成员变量了
我尝试通过 REST 从 JIRA 收集数据 API。
我可以用 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 = "";
}
创建一个 custom object
和 deserialize
的 json string
成这个对象的一个实例。我将在下面展示一个使用 gson
库
Maven 依赖
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
型号class
public class Model {
private boolean archived;
private String releaseDate;
private String name;
private String self;
private String userReleaseDate;
private long id;
private long projectId;
private boolean released;
//getters && setters ommitted
public Model(){
}
那你可以deserialize the JSON string into the Model class like this
String json = "{\"archived\":false,\"releaseDate\":\"2019-07-16\",\"name\":\"test 1.0\",\"self\":\"https://test/rest/api/latest/test/10000\",\"userReleaseDate\":\"16/Jul/19\",\"id\":\"10000\",\"projectId\":10000,\"released\":true}";
Gson gson = new GsonBuilder().create();
Model model = gson.fromJson(json, Model.class);
这样就不用每次都单独检查每个参数赋值给对应的成员变量了