为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用 GSON 获取 JSON 数据

如何使用 GSON 获取 JSON 数据

慕慕森 2021-09-03 16:38:44
之前可能已经问过这个问题,但我不知道我的问题的术语,因此不知道要查找什么。我正在使用 GSON 和 Java 试图从解析的 JSONElement 中获取信息。爪哇代码:    JsonParser parser = new JsonParser();    String url = "https://chapel-logs.herokuapp.com/attendance";    URL obj = new URL(url);    HttpURLConnection con = (HttpURLConnection) obj.openConnection();    // optional default is GET    con.setRequestMethod("GET");    //add request header    con.setRequestProperty("Accept", "application/json");    int responseCode = con.getResponseCode();    System.out.println("\nSending 'GET' request to URL : " + url);    System.out.println("Response Code : " + responseCode);    BufferedReader in = new BufferedReader(            new InputStreamReader(con.getInputStream()));    String inputLine;    StringBuffer response = new StringBuffer();    while ((inputLine = in.readLine()) != null) {        response.append(inputLine);    }    in.close();    //print result    JsonElement element = parser.parse(response.toString());    if (element.isJsonObject()) {        JsonObject albums = element.getAsJsonObject();        System.out.println(albums.get("students")); //prints out data in students        System.out.println(albums.get("students.LastChapelAttended")); //error    }我的 JSON :{"students":[{"LastChapelAttended":{"Loc":"","Chapel":"WhyChapel","Year":2018,"Month":9,"Day":6,"Hour":15,"Min":14,"Sec":28},"StudNum":"F02660934","Attendance":17},{"LastChapelAttended":{"Loc":"","Chapel":"WhyChapel","Year":2018,"Month":9,"Day":5,"Hour":19,"Min":49,"Sec":11},"StudNum":"002660934","Attendance":2},{"LastChapelAttended":{"Loc":"","Chapel":"WhyChapel","Year":2018,"Month":9,"Day":4,"Hour":20,"Min":35,"Sec":57},"StudNum":"002643472","Attendance":2},{"LastChapelAttended":{"Loc":"","Chapel":"WhyChapel","Year":2018,"Month":9,"Day":7,"Hour":5,"Min":34,"Sec":54},"StudNum":"002664906","Attendance":1}]}我试图获得的数据是:LastChapelAttended,但是LastChapelAttended在students. 在 JavaScript 中,students.LastChapelAttended如果有帮助的话,相当于我正在尝试的内容。提前致谢!
查看完整描述

3 回答

?
一只萌萌小番薯

TA贡献1795条经验 获得超7个赞

JsonObject jObj=(JsonObject)albums.get("students").getAsJsonArray().get(0);
System.out.println(jObj.get("LastChapelAttended"));

将其作为 JsonArray 获取,然后遍历该数组以获取 LastChapelAttended。


查看完整回答
反对 回复 2021-09-03
?
不负相思意

TA贡献1777条经验 获得超10个赞

首先,students不是对象而是数组。因此students.LastChapelAttended不应该在任何语言中工作。如果你想检索LastChapelAttended数组中的第一个学生students[0].LastChapelAttended应该可以工作。


我对gson不熟悉,但我认为你想做的是这样的:


if (element.isJsonObject()) {

    JsonObject albums = element.getAsJsonObject();

    JsonArray students = albums.getAsJsonArray("students");


    for (JsonElement student : students) {

        System.out.println(albums.getAsJsonObject().get("LastChapelAttended"));

    }

}


查看完整回答
反对 回复 2021-09-03
?
慕姐4208626

TA贡献1852条经验 获得超7个赞

Students 是一个 JsonArray LastChapelAttended 是一个 JSONObject 你可以通过调用得到它


json = (json data)

JsonParser parser = new JsonParser();

JsonObject rootObj = parser.parse(json).getAsJsonObject();

JsonArray studentsArray = rootObj.getAsJsonArray("students");

for (JsonElement stu : studentsArray) {

JsonObject student = stu.getAsJsonObject();

JsonObject LastChapelAttended = student.getAsJsonObject("LastChapelAttended");


}


查看完整回答
反对 回复 2021-09-03
  • 3 回答
  • 0 关注
  • 325 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信