2 回答
TA贡献2036条经验 获得超8个赞
获取值时,可以转换为字符串。像这样更改代码,看看是否有帮助。
JsonObject object = (JsonObject) new JsonParser().parse(new FileReader("C:\\MyData\\response.json"));
JsonArray issues = (JsonArray) object.get("issues");
JsonObject issues0 = (JsonObject) issues.get(0);
String issue_key = (String) issues0.get("key");//<---here
System.out.println("Value of key is -> " + issue_key);
更新
如果你想要所有的值,只需把它放在“for”中:
JsonObject object = (JsonObject) new JsonParser().parse(new FileReader("C:\\MyData\\response.json"));
JsonArray issues = (JsonArray) object.get("issues");
for(int i=0; i<issues.size(); i++){
JsonObject issue = (JsonObject) issues.get(i);
String issue_key = (String) issue.get("key");
System.out.println("Value of key" + Integer.toString(i + 1) + " is -> " + issue_key);
}
更新 2
“更新”和“创建”的数据不在“问题”内,它们位于“字段”内,以访问它们,您需要从“字段”中获取它们。您必须逐级进入内部才能访问变量:
JsonObject object = (JsonObject) new JsonParser().parse(new FileReader("C:\\MyData\\response.json"));
JsonArray issues = (JsonArray) object.get("issues");
for(int i=0; i<issues.size(); i++){
JsonObject issue = (JsonObject) issues.get(i);
String issue_key = (String) issue.get("key");
JsonObject fields = (JsonObject) issues.get("fields");
JsonObject project = (JsonObject) issues.get("project");
String project_name = (String) project.get("key");
String fields_updated = (String) fields.get("updated");
String fields_created = (String) fields.get("created");
System.out.println("Value of key" + Integer.toString(i + 1) + " is -> " + issue_key);
}
TA贡献1775条经验 获得超11个赞
您收到错误,因为您正在将 a 强制转换为 。所以而不是使用JsonPrimitive
JsonObject
JsonObject issues0data = (JsonObject) issues0.get("key");
你应该做的
String issues0data = issues0.get("key").getAsString(); System.out.println("Value of key is -> " + issues0data);
在这里,调用将调用方法。如果基元是,这将小心,并将其转换为 。getAsString()
JsonPrimitive.getAsString()
boolean/number/string
string
添加回答
举报