如何使用GSON获取JSON数据

时间:2018-09-07 06:21:56

标签: java json gson

这可能是以前问过的,但是我不知道我的问题的术语,因此也不知道要查找什么。

我正在使用GSON和Java,试图从已解析的JSONElement中获取信息。

Java代码:

    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,但是LastChapelAttendedstudents之内。在JavaScript中,如果有帮助,相当于我正在尝试的方法students.LastChapelAttended

谢谢!

4 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:1)

首先,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"));
    }
}

答案 2 :(得分:1)

您可以在Difference between JSONObject and JSONArray上查看该帮助

学生是一个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");

}

答案 3 :(得分:0)

只需遍历学生数组

albums.getAsJsonArray("students")
      .forEach(student -> System.out.println(student.getAsJsonObject()
                            .get("LastChapelAttended")));